I’m using MVC 6 and would like to be able to access a particular namespace globally from all of my Razor views. In MVC 5 this was fairly simple; I’d just add the following code to my ~/views/web.config file:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
<add namespace="MyProject.WebUI" />
<add namespace="MyProject.WebUI.Helpers" /><!-- Added this line -->
</namespaces>
</pages>
</system.web.webPages.razor>
Where I’ve added access to the MyProject.WebUI.Helpers namespace.
In ASP.NET 5, and therefore MVC 6, the web.config file has be done away with, so I’m not sure how to go about doing this any more. I’ve tried searching for an answer, but all I can find is how to do it in current versions of ASP.NET rather than v5.
Any ideas?
Edit: Clarified which web.config file I would have used.
For <= beta3 bits (what you're most likely using) you should add an #using statements to your _ViewStart.cshtml. Aka:
_ViewStart.cshtml: #using MyProject.WebUI.Helpers
If you don't have a _ViewStart.cshtml you can create one and just make sure it's in the same path or parent path of the view you want it to affect.
For beta4 bits, this functionality was moved to a new file called _GlobalImport.cshtml; _ViewStart.cshtml was transitioned back to its original functionality (just running code, not inheriting directives). Therefore:
_GlobalImport.cshtml: #using MyProject.WebUI.Helpers
For beta5 bits, _GlobalImport.cshtml was renamed to _ViewImports.cshtml
Add your namespaces to the_ViewImports.cshtml file (it's under the Views folder).
Example file:
#using Microsoft.AspNetCore.Identity
#using Jifiti.Registry.Web.Models.AccountViewModels
#using Jifiti.Registry.Web.Models.ManageViewModels
#addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
I'm on beta 7 and I had to use
#using System.Security.Principal
#using System.Security.Claims
#Context.User.GetUserId()
Related
Using Visual Studio 2015 & Sitecore 8.0
I have created a new MVC project with .Net Framework 4.6.1, copied, and referenced the Sitecore.kernel.dll & Sitecore.mvc.dll. Replaced the existing web.config & global.asax with the sitecore files.
Added a plain HTML page in the solution and tried to browse it (from VS). The page opens with this error
Could not load file or assembly 'System.Web.Http, Version=5.1.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its
dependencies. The system cannot find the file specified.
This HTML page is not published to Sitecore yet. I'm guessing, the Sitecore.mvc.dll wants MVC 5.1.0 and the MVC in VS project is 4.0.
So, I tried the following steps.
Run install package command in package manager console for MVC 5.1.1 which shows the error
Unable to load the service index for source
https://api.nuget.org/v3/index.json.
Deleted the %AppData%\NuGet\nuget.config and restarted VS2015
Manually browsed https://api.nuget.org/v3/index.json, but the page doesn't load - Secure connection failed. But this - http://api.nuget.org/v3/index.json will promt to download the json. So, in the nuget changed https to http, but its the same error - Unable to load the service index...
I'm not even able to start my first Sitecore MVC project when this happens.
I replaced the sitecore web.config with the regular VS project's web.config and now I'm able to browse the page. I set its Copy To Output Directory to Do not copy, so that it will not get published.
Will I be able to proceed with my Sitecore development this way without any major issues in future.
You need to do a few things to fix this:
Your VS project should be updated to have the web.config file from the Sitecore installation. You must have the Sitecore version of that file or Sitecore will not run correctly
You should add a binding redirect for the MVC file that makes everything use the latest version:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
</assemblyBinding>
etc....
</runtime>
Make sure that the web.config file in your /views folder contains the correct versions of all the MVC assemblies. This is mine for a .net 4.6 application:
<configSections>
<sectionGroup name="system.web.webPages.razor"
type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host"
type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
requirePermission="false" />
<section name="pages"
type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host
factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization" />
<add namespace="System.Web.Routing" />
<add namespace="Sitecore.Mvc" />
<add namespace="Sitecore" />
</namespaces>
</pages>
</system.web.webPages.razor>
Once that is done, your Sitecore installation should pick up the latest MVC binaries correctly.
I installed a sitecore instance with SIM, and checked the project for a MVC solution, what I don't get is why the #Html.Sitecore() is not working:
Error 2 'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'Sitecore' and no extension method 'Sitecore' accepting a first argument of type 'System.Web.WebPages.Html.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)
I have the /Views/Web.config with the following:
<namespaces>
<add namespace="Sitecore.Mvc" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
Is there something different that needs to be done when running a solution inside the "Website" folder vs publishing to the Website folder?
I'm trying to precompile razor views but during the build VS2010 throws errors such as "#ViewBag does not exist in the current context." The project builds and runs fine when views are not precompiled.
In order to turn on view compilation I set the following node in the project file:
<MvcBuildViews>true</MvcBuildViews>
The section below exists in the Views Web.config file.
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Xml"/>
<add namespace="System.Xml.Linq"/>
</namespaces>
</pages>
</system.web.webPages.razor>
Any ideas why the project won't build?
Geeze, do I ever feel like an idiot. Turns out there were some cshtml files in a _temp folder that resided inside of the project folder. Even though the folder was excluded from the solution the compiler was picking up the files contained therein. Moving that _temp file out of the project folder fixed the issue.
I have just installed the Visual Studio 2013 preview and run up my site. I've noticed that the less files which are used in my site are not being correctly transformed to css and are coming down as blank CSS files.
It appears something is going wrong inside the dotless httphandler as when I force minification for the bundle everything works correctly.
web.config
<section name="dotless" type="dotless.Core.configuration.DotlessConfigurationSectionHandler, dotless.Core" />
...
<httpHandlers>
<add path="*.less" verb="GET" type="dotless.Core.LessCssHttpHandler, dotless.Core" />
</httpHandlers>
...
<handlers>
<add name="dotless" path="*.less" verb="GET" type="dotless.Core.LessCssHttpHandler,dotless.Core" resourceType="File" preCondition="" />
</handlers>
The issue appears to go away if I force minification eg. System.Web.Optimization.BundleTable.EnableOptimizations = false; which suggests something is different in the way it processes the HTTP handler
Try putting handleWebCompression="false" in the configuration for dotless (in the web config)
<dotless minifyCss="false" cache="true" web="false" handleWebCompression="false" />
Similar to this question
I am porting a web application to 4.0. It has a root web.config that adds a namespace:
<namespaces>
<add namespace="Insignia.Catalog2"/>
...
</namespaces>
There is a sub-folder to this app with its own web.config:
<namespaces>
<remove namespace="Insignia.Catalog2"/>
<add namespace="Insignia.Catalog"/>
</namespaces>
.Catalog and .Catalog2 have overlapping class names. This worked great in ASP.NET 2.0 thru 3.5 but seems broken in 4.0 - that is, I now get compiler errors about the colliding class names.
Well I figured it out. While the 'remove' tag WAS removing .Catalog2, another DLL had changed to include a reference to that namespace. Using a control from that other DLL brought .Catalog2 along with it! Fixing the reference solved the problem.