MVC Razor view Intellisense broken in VS 2013/2015/2017 - asp.net-mvc

I have an existing project written in VS2010 which when loaded in VS2010 works perfectly.
When I load this same project in VS2013 the MVC Razor views contain lots of errors as if the config file is missing from the views folder.
It appears to have not loaded the Razor editor correctly using the config files from both the root and the views folder and instead gives me errors like ...
The name 'model' does not exist in the current context
and ...
'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'LabelFor'
and no extension method 'LabelFor' accepting a first argument of type
'System.Web.WebPages.Html.HtmlHelper' could be found
(are you missing a using directive or an assembly reference?)
...
Any idea what would cause this?
Edit:
Config files as requested ....
From main web.config file (not all of it as it's way too big to post)
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Ninject" culture="neutral" publicKeyToken="c7192dc5380945e7" />
<bindingRedirect newVersion="3.0.0.0" oldVersion="0.0.0.0-3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Practices.EnterpriseLibrary.Validation" culture="neutral" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect newVersion="5.0.505.0" oldVersion="0.0.0.0-5.0.505.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Practices.ServiceLocation" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.2.0.0" newVersion="1.2.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Config file in "~/Views/" ...
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.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=3.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="Emedia.Common.Mvc.Views.Helpers"/>
<add namespace="Emedia.Common.Mvc.Views.Extensions"/>
<add namespace="Emedia.Common.Utilities"/>
<add namespace="Emedia.Common.Utilities.Extensions"/>
<add namespace="Emedia.Common.Mvc.Controllers.Helpers"/>
<add namespace="Emedia.Resources.Service"/>
<add namespace="Emedia.Subscriber.Controllers"/>
<add namespace="Emedia.Subscriber.Controllers.ViewModels"/>
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
<system.web>
<httpHandlers>
<add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>

I considered editing #ChrisMoschini's post, but thought it was different enough. My issue was that I started a new MVC5 application, and blindly copied over too many web.config settings from an old MVC3 project I wanted to use as a template/starting point. Doing this caused me to have some invalid versions referenced in my web.config.
To fix, I created another new MVC5 project and made sure the following config values in my bad project matched the vanilla, unmodified MVC5 app. Again, do not blindly copy these version numbers. Just make sure they match a vanilla MVC app of the version that you're trying to get to work
in root web.config:
<appSettings>
...
<add key="webpages:Version" value="3.0.0.0"/>
...
</appSettings>
<system.web>
...
<compilation debug="true" targetFramework="4.5.1"/>
<httpRuntime targetFramework="4.5.1"/>
...
</system.web>
in the Views\Web.config:
<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>
<system.web.webPages.razor>
...
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
...
</system.web.webPages.razor>

A few of my projects had this issues for months. I tried so many workarounds and nothing worked. The issues seemed to be with the projects: the same project will have Intellisense issue across all PCs with Visual Studio. Finally I came through this post: http://www.dennisonpro.info/managing-intellisense-in-razor-views-with-mvc-5-using-custom-builds-in-visual-studio-2013/
In our case the cause was the output directory of all those projects were changed to other than "bin\". By changing the OutputPath back to "bin\" (and cleaning the project, closing then re-opening Visual Studio), I got Intellisense back.
The post referenced above also provided a workaround to maintain a separate output directory while still having Intellisense.
Hope this helps someone someday.

MS says that for VS2013 "Intellisense for Razor (CSHTML and VBHTML) files is limited to HTML markup."
But if you add these two lines inside each .cshtml the intellisense will work again for MVC3 in VS2013:
#using System.Web.Mvc.Html
#inherits System.Web.Mvc.WebViewPage<dynamic>
Instead of dynamic you can put your Model's type.

I upgraded an MVC3 project to MVC5, and did everything I could to avoid the only true answer to this problem that Microsoft recommends:
Start over with a new Project, and port the resulting bits over into your existing project.
That process is a big waste of time, and it seems like Microsoft should make a real upgrade path viable, but I am here to tell you the problem here is an emotional one not technical: You really do need to just create a new MVC5 project, and replace the following with the result of that new MVC5 project:
\Packages\*
\Project\Project.csproj
\Project\packages.config
\Project\Web.Config
\Project\Views\Web.Config
\Project\Areas\*\Views\Web.Config
If you don't you'll just go around in circles for eternity trying to find the one setting that's blowing things up. In our case, I had the Web.Configs all identical to the newly-created, Razor Intellisense-working MVC5 project, I had run every variety of upgrade tool I could find, you name it. Intellisense refused to work.
After just blowing away the .csproj and web.configs etc, Intellisense came magically back to life. Diffing the 2 sides, none of the answers that I've found anywhere match with what I'm seeing. Web.Configs are almost identical, and the bits that are different should be irrelevant. The main change is really what's in \Packages\ - a lot of older Razor, MVC, and WebPages dlls gone. That could be the trick, but skip that and save yourself a lot of time: Just make a new MVC5 project and dump the above-listed files over. It's the only sane method of upgrading.

I have recently solved this problem myself. I upgraded from MVC4 to MVC5 (specifically 5.1). Upgrading to a newer version of MVC caused this havoc and I spent hours trying to solve it. Minor changes to the Web.Config file fixed the intellisense issue!
You said the project works in VS2010, but not 2013? See this answer here.
I recommend upgrading to MVC5. It's not painful and the upgrade should be pretty seamless.
If you upgrade to MVC5 and you're still not getting intellisense, you need to update the Web.Config file manually as the upgrade may not do this correctly!
Here's a modified version of your Web.conifg in the /Views folder that should reflect changes for MVC5.
<?xml version="1.0"?>
<configuration>
<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.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.Routing" />
<add namespace="Emedia.Common.Mvc.Views.Helpers"/>
<add namespace="Emedia.Common.Mvc.Views.Extensions"/>
<add namespace="Emedia.Common.Utilities"/>
<add namespace="Emedia.Common.Utilities.Extensions"/>
<add namespace="Emedia.Common.Mvc.Controllers.Helpers"/>
<add namespace="Emedia.Resources.Service"/>
<add namespace="Emedia.Subscriber.Controllers"/>
<add namespace="Emedia.Subscriber.Controllers.ViewModels"/>
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
<system.web>
<httpHandlers>
<add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
If upgrading is not an option, then I'm afraid you will need to continue using VS2010 if you want intellisense support for MVC3. However, I strongly suggest upgrading to MVC5.

For anyone else who might stumble across this I didn't find any of the previous answers to really solve my problem or not pertain to it. Anyways the way that solved this for me and made intellisense work again was to go to my bin folder and delete all the files in there and then clean/rebuild and it was fixed.

When I created a new project intellisense worked fine, but for some reason it didn't work in our current project. The only difference I found in the Views/Web.config file was that ours had MVC version 5.2.0.0 and a new project had 5.0.0.0. What worked for us was to change this:
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc,
Version=5.0.0.0, Culture=neutral, PublicKeyToken=123JHJF56AD364E35" />
to this:
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc,
Version=5.2.0.0, Culture=neutral, PublicKeyToken=123JHJF56AD364E35" />

This question has been resolved, but I'm adding this for future folks, since none of the above worked for me:
Try running Visual Studio as an administrator.
Somehow when I tried to delete my nuget packages (which contain all the required references, such as System.Web.Mvc, I was told I need permission from MyPC\Me. Ridiculous! (I am the only user and only admin...) In any case, running as an admin at least let me access those files which fixed intellisense.

A combination of the following helped to solve the problem for me:
Creating a new MVC Project
Comparing the versions in the views web.config files of the existing and new projects
Fixing the versions accordingly (see below)
Deleting all files in the bin folder
Cleaning the solution
and finally rebuilding the solution
<configuration>
<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" />
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Version" value="3.0.0.0"/>
<add key="webpages:Enabled" value="false" />
</appSettings>
[...]

I have had this over and over again; I'm on my third new project, and it's driving me mad! I think I may have found the reason for the problem. I'd added files, but not included them in Visual Studio. So I've now chosen to view all files:
Then for all my folders which I'd added, I include them:
The problem is that this only seems to show up when you open an MVC site as a project, and not when you open it as a website.

I eventually had a bunch of other problems on my pc because of a network server crash and ended up reinstalling visual studio.
This apparently solved the problem ... i have no idea how but it did.
I wonder if maybe in my case it was simply just a faulty installation rather than the typical problem.
For that reason I will mark this as the answer but +1 all other replies as they are potentially good answers to this problem.
I did however find that Microsoft clearly states visual studio 2013 does not support intellisense on lower versions of MVC than 4 so if you are using MVC 3 upgrade your project if you are using a newer version of MVC and nothing else here works try reinstalling visual studio.
Also worth noting ...
MVC is now a nuget package so don't install MVC from the download redist allow VS to figure that out for you.

In my case I moved all of the views from an Area to the root Views folder, so I think VS got confused as to where my web.config was. I renamed it to Web.config, from web.config and then made an edit to the contents of the config (such as changing the version number of the razor host factory dll from 5.2.2.0 to 5.2.3), but then changed it back.
Then I went on a walk for about 30 minutes and came back and restarted VS and it was fixed!

In MVC 5 if you try to add an area by just adding a folder under Areas and sub folders for Controllers, Views, Models, etc. you won't have the *AreaRegistration.cs file that registers the area, or the web.config in views that enable VS to understand the razor elements that you include in your views. The result is that intellisense doesn't work in your view for things like ViewBag. If you have working examples of those files in other areas you can copy them in and update as appropriate - or you can start over with your Area by right clicking on Areas and doing an Add - Area which will create those files for you.

I've changed from
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc,
Version=5.2.0.0, Culture=neutral, PublicKeyToken=123JHJF56AD364E35" />
to this:
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc,
Version=5.0.0.0, Culture=neutral, PublicKeyToken=123JHJF56AD364E35" />
And it worked!

Update the NuGet Packages using Package Manager console in Tool, Library package Manager..
In command line..
PM> Update-Package
This will update NuGet packages and verify the current version of System.web.MVC and update this version in web.config file under Views folder.
<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="System.Web.Webpages"/>
</namespaces>
</pages>
</system.web.webPages.razor>
Close the solution and Reopen.
Hope this works !!!

I had this issue for six months and just realized that I needed to move my #model directives to the top of the pages. I previously had them inside code blocks.

I am using visual studio 2012.
I tried all of the above but finally the problem is resolved by installing :
ASP.NET and Web Tools 2013.1 :
http://blogs.msdn.com/b/webdev/archive/2013/11/18/announcing-release-of-asp-net-and-web-tools-2013-1-for-visual-studio-2012.aspx

I ran into a similar problem. I had an MVC 5 project created with VS2015 Community Edition that I needed to work on with VS2013 Ultimate. Removing the following <system.codedom></system.codedom> block from the root web.config file is what finally allowed IntelliSense to work again in my Razor views on VS2013.
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
</compilers>
</system.codedom>

Try setting the CopyLocal property of the System.Web.Mvc reference to true. Sometimes this helps with Intellisense.

I am using VS 2017 professional, and I tried almost every answer on this post, and also those on that post, but nothing worked for me. Yesterday I updated VS, to version 15.2 (26430.6) Release, and intellisense is back in my cshtml files!

I have tried almost every solutions but did not get intellisense and at the end I found a solution:
Go to Solution Explorer
Right click on .cshtml file or any view file
Select "Open With" option and make HTML Editor(Default) set as default

In upgrading from MVC 3 to 5, I found that in my root directory Web.config that the appSettings key webpages:version was set to 2.0.0.0. Changing this to 3.0.0.0 fixed this issue.
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
</appSettings>

Related

'System.Web.Webpages.Html.Htmlhelper' does not contain a definition for 'Sitecore'

I'm getting this error in Visual Studio, when I use #Html.Sitecore:
'System.Web.Webpages.Html.Htmlhelper' does not contain a definition for 'Sitecore' and the best extension methods overload 'Sitecore.Mvc.HtmlHelperExtensions.Sitecore(System.Web.Mvc.HtmlHelper)' has some invalid arguments.`
However, once deployed it does run without any problems.
I'm using Sitecore 7.2 with MVC 5.1.
Articles that I've read that deal with similar error messages, talk about the system.web.webPages.razor section of the Views folder web.config file. This is how it appears in my solution.
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.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" />
</namespaces>
</pages>
Originally I thought I'd just need to restart VS, but that didn't work. Does anyone have any suggestions.
EDIT
This is the standard Sitecore 7.2 installation, and so has the following binding redirects:
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" xmlns="urn:schemas-microsoft-com:asm.v1"/>
<bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.1.0.0" xmlns="urn:schemas-microsoft-com:asm.v1"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35" xmlns="urn:schemas-microsoft-com:asm.v1"/>
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" xmlns="urn:schemas-microsoft-com:asm.v1"/>
</dependentAssembly>
Also when looking at the output from visual studio there is an additional error that I didn't spot before:
Instance argument: cannot convert from 'System.Web.WebPages.Html.HtmlHelper' to 'System.Web.Mvc.HtmlHelper'
EDIT 2
I no longer think this is a Sitecore MVC issue. I get a similar error with #Html.ActionLink("xxx", "xxx"). Other people have seen this issue in VS2012, but I'm using 2013.
EDIT 3
There are now quite a few answers to this question. I recommend trying each of them as it seems there are numerous reasons this error might occur.
I have the same problem a while ago. We found out that we are missing the web.config inside /views folder. we copy a web.config from my other projects' /views and it solved the issue.
Try This, then try to restart visual studio.
<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="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>
</pages>
</system.web.webPages.razor>
You forgot to add <add namespace="Sitecore.Mvc" /> so that intellisense can pick it up in VS.
I have now resolved this issue. I simply had to install Update 2 of Visual Studio 2013. How frustrating.
Thanks to StriplingWarrior and Ahmed Okour for your useful advice.
I have used the below line at the top and the issue has been resolved.
#inherits System.Web.Mvc.WebViewPage
You say you're using MVC 5, but you're referencing version 3 in the code you provided. Here's what mine says:
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
Try checking through your entire web.config file (or all the web.config files if you have multiple Areas) and making sure that all the versions are set right. For MVC 5, System.Web.WebPages should be on version 2 and System.Web.WebPages.Razor should be on version 3.
<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>
...
<assemblies>
<add assembly="System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
<add assembly="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
Sometimes its the silly things. Check you're not doing something like
#Html.Sitecore("placeholdername") // no method
or
#Html.Sitcore.Placeholder("placeholdername") // Missing parenthesis
when you should be doing
#Html.Sitecore().Placeholder("placeholdername") // this works
<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="Kendo.Mvc.UI" />
<add namespace="Telerik.Reporting" />
</namespaces>
I have added Sitecore.Mvc dll in references with copy local to true and added below tag in view's web.config under namespaces tag, then my problem is resolved.

ASP.NET MVC 4 Project - Razor intellisense has been lost

I've recently updated an ASP.NET MVC project from v3 to v4, and now my .cshtml Razor views have lost intellisense when edited in Visual Studio. Keywords and helper properties such as #model, #Url, #Html etc. are not being recognised and are producing errors in VS. When ran, the app itself works fine.
I've tried suggested solutions from various online sources, including SO, most of which involve tweaks to the web.config. I've been over them and I'm pretty sure my config settings are correct for MVC 4:
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.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=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.Optimization"/>
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
<system.web>
<httpHandlers>
<add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</configuration>
Now, I have made some observations which give me an inkling what the problem might be. If I go through my web.config and revert the references to MVC 3, i.e. MVC from version 4.0 to 3.0, and Razor/WebPages from 2.0 to 1.0, my view intellisense comes back (but obviously this leaves me with a non-functioning app).
I think I may possibly have an installation issue and Visual Studio, for whatever reason, is unable to locate the assemblies specified in the web.config for the purposes of Razor intellisense. I installed the ASP.NET MVC 4 reference via NuGet, and I've double-checked that all my referenced assemblies are the correct version. I've tried re-installing the MVC package and I've tried cleaning and rebuilding several times.
Thanks in advance for any suggestions as to how I can get this working. It's really hampering my productivity at the moment.
I think you need the System.Web.Helpers namespace and possibly System.Web.WebPages:
Change this:
<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" />
</namespaces>
to:
<namespaces>
<add namespace="System.Web.Helpers" />
<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="System.Web.WebPages" />
</namespaces>
The solution which worked for me (taken from the release notes at http://www.asp.net/whitepapers/mvc4-release-notes):
In Solution Explorer, right-click the project name and then select Unload Project. Then right-click the name again and select Edit ProjectName.csproj.
Locate the ProjectTypeGuids element and replace {E53F8FEA-EAE0-44A6-8774-FFD645390401} with {E3E379DF-F4C6-4180-9B81-6769533ABE47}.

Razor View throwing "The name 'model' does not exist in the current context"

After significant refactoring in my MVC 4 application, and Razor shows this error while debugging Views:
The name 'model' does not exist in the current context.
This is the offending line of code:
#model ICollection<DataSourceByActive>
I know that the usage of #model is correct.
Why is this happening? How can I fix it?
I think you have messed up the web.config file which lives in the Views folder.
Create a new project targeting the same .NET framework and copy its Views/web.config file on top of the one in your current project. This will fix your problem.
Also, as Dudeman3000 commented, if you have Areas in your MVC project they all have Views\web.config files too.
Make sure you have the following in both your site Web.config and views directory Web.config in the appSettings section
<add key="webpages:Version" value="2.0.0.0" />
For MVC5 use:
<add key="webpages:Version" value="3.0.0.0" />
(And it only exists in the main Web.config file.)
Here is what I did:
Close Visual Studio
Delete the SUO file
Restart Visual Studio
The .suo file is a hidden file in the same folder as the .svn solution file and contains the Visual Studio User Options.
I had the same issue, I created a new project and copied the web.config files as recommended in the answer by Gupta, but that didn't fix things for me. I checked answer by Alex and Liam, I thought this line must have been copied from the new web.config, but it looks like the new project itself didn't have this line (MVC5):
<add key="webpages:Version" value="3.0.0.0" />
Adding the line to the views/web.config file solved the issue for me.
Changing following line in web.config of view folder solved the same error.
From
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
To
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
I was using an MVC4 project with Visual Studio 2019 - and it turned out VS 2019 does not support MVC 4 out-of-the-box. You have to install this.
Steps:
Open Visual studio installer (Search for Visual Studio Installer in windows)
Click individual components
Write "mvc" in the search box
Check the mvc4-box
Click "Modify" at the bottom right
NOTE: Required for visual studio to be closed
Changing to #Model from #model did the job for me.
#model represents the View Model object type. #Model represents the View Model object.
In my case, I recently updated from MVC 4 to MVC 5, which screws up the web.config pretty badly. This article helped tremendously.
http://www.asp.net/mvc/overview/releases/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2
The bottom line is that you need to check all your version number references in your web.config and Views/web.config to make sure that they are referencing the correct upgraded versions associated with MVC 5.
I've found a solution.
If you want to update razor version or mvc 4 to 5, change some lines.
Old code in Views/web.config
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
Replaced with
<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>
sectionGroup must be change, too.
In my case, the following code founds to be useful. Place below code in Web.config file under Views folder.
<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>
Once the code is updated, make sure to clean and rebuild the solution. I hope this will help you out!
None of the existing answers worked for me, but I found what did work for me by comparing the .csproj files of different projects. The following manual edit to the .csproj XML-file solved the Razor-intellisense problem for me, maybe this can help someone else who has tried all the other answers to no avail. Key is to remove any instances of <Private>False</Private> in the <Reference>'s:
<ItemGroup>
<Reference Include="Foo">
<HintPath>path\to\Foo</HintPath>
<!-- <Private>False</Private> -->
</Reference>
<Reference Include="Bar">
<HintPath>path\to\Bar</HintPath>
<!-- <Private>True</Private> -->
</Reference>
</ItemGroup>
I don't know how those got there or exactly what they do, maybe someone smarter than me can add that information. I was just happy to finally solve this problem.
For some reason my web.config had 0.0.0.0 in the oldVersion attribute:
<runtime>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
</runtime>
changing to 1.0.0.0 was the solution:
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0"/>
</dependentAssembly>
I had the same problem when deploying to an Azure App Service
In my case it was because ~/Views/Web.config wasn't included in the project.
It worked in IIS Express but when I deployed to azure, I got the same error. By not being included in the .csproj file, it wasn't deployed.
The solution was to ensure ~/Views/Web.config is included in the project.
If you go to solution explorer and click the "Show all files" icon, then open up Views you might see an unincluded Web.config file under there.
Add it in, re-publish, and bob's your uncle.
In my case, the issue was that after upgrading the project from MVC 4 to MVC 5 I somehow missed a version change in the Views/web.config:
<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">
It still had the old 2.0.0.0 version. After changing the version to 3.0.0.0 everything started working just right.
Also, because of this problem, Visual Studio 2015 Community Edition would start bashing the CPU (30-40% usage at idle) every time I would open a .cshtml file.
For me, the issue was a conflicting .NET version in one of the libraries that I recently imported. The library I imported was compiled for 4.5.2 and the ASP.NET MVC site I imported it into targeted 4.5. After recompiling said lib for 4.5 the website would comppile.
Also, there were no compilation errors, but the issue was being reported as a "warning". So be sure to read all warnings if there are any.
if you take this problem without any change on your project as like as me,
you need change your web.config that placed in View Folder.
just write new line by Enter or Remove an empty line . then save your web.config and rebuild.
my problem solved with this solution
In order to solve this I made sure that I upgraded to the newest MVC version using NuGet and Package Manager Console.
Install-Package Microsoft.AspNet.Mvc -Version 5.2.4
Then upgraded to the latest Razor version
Install-Package Microsoft.AspNet.Razor -Version 3.2.4
Then I changed all the web.config files to reflect the change. As you will see below:
In the main web.config file, make sure that the value for the webpages:Version key is correct. This is where it can be found (ignore the other keys):
<configuration>
<appSettings>
<add key="webpages:Version" value="3.0.0.0"/>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>
</configuration>
Then look for the other versions listed in the assemblies, check the Version of the assembly against the version of the library listed in your project references! You may not need all of these.
<system.web>
<compilation debug="true" targetFramework="4.6">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Helpers, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.WebPages, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>
</compilation>
</system.web>
runtime assemblyBinding should show the "newversion" as well, see where it reads newVersion 5.2.4.0? But also check all the other versions.
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-5.2.4.0" newVersion="5.2.4.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
THEN in the Views Web.Config section, make sure that System.Web.WebPages.Razor is the correct version:
<configuration>
<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>
<configuration>
And lastly there is the Pages section of the Views Web.Config
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
I was trying to add a view which were outside of my "Views" folder (just to organize my code differently, I guess), when I had this issue. Creating the view inside Views (as by convention) solved it.
In my case, I removed web.config file from Views folder by accident. I added it back , and it was OK.
There appear to be 3 version-number settings which need to be correct in relation to each other here:
... System.Web.Mvc, Version=x.x.x.x ... (in various places ~\Views\web.config)
... System.Web.WebPages.Razor, Version=x.x.x.x ... (in various places ~\Views\web.config)
<add key="webpages:Version" value="x.x.x.x" /> (in ~\web.config) NB: root web.config
Combinations that have worked for me:
Combination 1:
System.Web.Mvc, Version=4.0.0.0
System.Web.WebPages.Razor, Version=2.0.0.0
<add key="webpages:Version" value="2.0.0.0" />
Combination 2:
System.Web.Mvc, Version=5.2.7.0
System.Web.WebPages.Razor, Version=3.0.0.0
<add key="webpages:Version" value="3.0.0.0" />
A final observation is that the webpages:Version setting appears to be optional. Removing it appears to have no negative consequences, at least in the context of the issue at hand.
I had this problem in nopCommerce where I was copying a view part of a plugin to the output folder under nopcommerce\Plugins, but the view needed to be an embedded resource. So the wrong Build Action for the .csthml file was "Content" and where it should be "Embedded Resource", no copying needed.
It has been 7 years. However, my situations are little bit different.
All the views are working fine. Therefore, changing web.config and nuget to get new libraries would not work and possibly that would have introduced the problem.
My scenario was I have been working on Project B. Project B is cloning of Project A. Long Story short, a lot time, to save the time, I have to copy codes from project B to project A.
This time, I just copied the file, abc.cshtml, into Project A from project B. Project B was developed using 2015. For some reason, this caused problem. I don't know. Anyway, I removed the abc.cshtml and create the blank abc.cshtml from scratch. Then I select every everything on abc.cshtml in Project B, and copy all the texts to project A newly created abc.cshtml.
I solved my problem
Using an invalid expression can trigger such a compiler error. In the following scenario, the intended lambda expression should be =>, not ==>
In my case I was missing # at the beginning of the foreach
#foreach (var item in Model)
{
<tr class="clickable-row">
<td class="clickable-field">
#Html.DisplayFor(modelItem => item.Label)
</td>
<td class="clickable-field hidden-xs">
#Html.DisplayFor(modelItem => item.Value)
</td>
</tr>
}
I solved the problem by using #Model instead of just model when printing the variables.
You are likely to use in the code a variable named model.

Developing ASP.NET MVC using MonoDevelop on MAC

I am developing a web app using ASP.NET MVC 2.0 in MonoDevelop. I have latest Mono 2.10 and MonoDevelop 2.4.2 and I am running them on Mac.
According to mono’s release notes, it should support ASP.NET MVC 2.0. However, I found MonoDevelop’s project templates still only support ASP.NET MVC 1.0. So I had to create MVC project from MonoDevelop and remove system.web.mvc 1.0.0.0 from reference and add system.web.mvc 2.0.0.0 back in. I can write and compile code OK and MonoDevelop does provide intellisense for methods only exist in MVC 2.0 from both C# and aspx file. However, when I trying to run it from MonoDevelop, I had Compilation error complaining ”<%: Html.LabelFor(model => model.Name) %>” as LabelFor method cannot be recognized .
MonoDevelop uses XSP as its testing web server. I am not sure how it configured as I didn’t install it myself. I think it was installed as part of MonoDevelop. My guess is XSP somehow still uses MVC 1.0 but I can't prove it as I couldn’t even find where it is installed.
Could anyone help me out please?
Update:
I have been suspecting web.config too. Here are how they look like now:
web.config
<configuration>
<system.web>
<compilation defaultLanguage="C#" debug="true">
<assemblies>
<add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</assemblies>
</compilation>
<customErrors mode="RemoteOnly">
</customErrors>
<pages>
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
<authorization>
<allow users="*" />
</authorization>
web.config under views
<configuration>
<system.web>
<httpHandlers>
<add path="*" verb="*" type="System.Web.HttpNotFoundHandler" />
</httpHandlers>
<pages validateRequest="false" pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="BlockViewHandler" />
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
Setting up an ASP.NET project for ASP.NET MVC requires changing and adding a load of settings in web.config. The project template in MD is for ASP.NET MVC 1.0, not ASP.NET MVC 2, so you're probably missing some of the necessary settings, maybe a namespace import.
I have this running with two of my current projects on MacOS X. There were only three steps I needed to make sure were done, they are as follows:
Upgrade the version on all references in both the inner (views) web config and the outter(app) [you've done this].
Dereference System.Web.MVC 1.0.0.0 & Reference 2.0.0.0 for the project
Ensure you don't have 1.0.0.0 binaries in your bin path, the public key does not change
I've done a downloadable zip file of a project template for Asp.Net MVC 4 which builds and runs on/in Mono/Xamarin Studio/MonoDevelop on Mac & Linux at http://www.cafe-encounter.net/p1319/run-asp-net-mvc4-on-mono-monodevelop-on-mac-the-c-template-project.
I should add that a lot of the issue resolution came from Does the Razor View Engine work for Mono?

Need razor view engine auto-complete to work in a class library?

We have a modular architecture where we have some views (cshtml) files in a separate project (class library). How can we get the syntax highlighting and autocomplete to work when the project isn't an MVC project?
Please note that the class library has controllers, views, models etc. It just doesn't have the web.config, global.asax, etc that a normal mvc project would have.
The intellisense works for everything but the so important model:
With MVC3 RTM, if you hover over the Model, you can now get a better error message:
C:\...\Index.cshtml: ASP.NET runtime
error: There is no build provider
registered for the extension
'.cshtml'. You can register one in the
<compilation><buildProviders>
section in the machine.config or
web.config. Make sure is has a
BuildProviderAppliesToAttribute
attribute which includes the value
'Web' or 'All'.
So I added this:
<compilation>
<assemblies>
<add assembly="System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
<buildProviders>
<add
extension=".cshtml"
type="System.Web.WebPages.Razor.RazorBuildProvider, System.Web.WebPages.Razor"/>
</buildProviders>
</compilation>
Then after adding the build provider, this error message appears:
C:\...\Index.cshtml: ASP.NET runtime
error: Could not load file or assembly
'System.Web.WebPages.Razor' or one of
its dependencies. The system cannot
find the file specified.
(C:\...\machine.config line 259)
The webconfig from this post will work. I've copied it below (for posterity):
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.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=3.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" />
</namespaces>
</pages>
</system.web.webPages.razor>
<system.web>
<compilation targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</assemblies>
</compilation>
</system.web>
</configuration>
For Visual Studio 2012/ASP.NET MVC 4, you need to update the assembly versions and add <add key="webpages:Version" value="2.0.0.0" /> to appSettings. Here's what my Web.config looks like:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.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=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" />
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
</appSettings>
<system.web>
<compilation targetFramework="4.5">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
</system.web>
</configuration>
I have followed Jammer's suggestion and am documenting what I believe is the minimal set of actions to get a project suitable for serving as an ASP.NET MVC4 class library project. This was done in Visual Studio 2012 Update 4, and was targeting VB.Net. I may later include documentation for doing something similar in Visual Studio 2013 if I get a chance. Here are the actions I took:
Create a new blank MVC4 project with a separate directory for the solution (so you can clearly see that the packages directory with updated versions of reference files is part of the project).
Add a dummy controller, view and model to test it (which entailed copying the output DLL and view to another web application after building the class library).
Delete the following files from the project:
The whole App_Start folder
The whole App_Data folder and any other empty folders (my Mercurial history didn't make this visible so I'm going from memory).
Global.asax
Global.asax.vb
The Web.config file in the root and the dependent Web.Debug.config and Web.Release.config files. (Do not delete Web.config from the Views folder.)
Delete the following sections from the Web.config file in the Views folder:
appSettings
system.web
system.webServer
You may also delete lines <add namespace="System.Web.Mvc.Ajax" /> and <add namespace="System.Web.Routing" />
Remove the following references from the project (* starred references were version-specific references that went into the packages directory and have Copy Local and Specific Version set to True).
System.Web.Entity
System.Web.ApplicationServices
System.ComponentModel.DataAnnotations
System.Data.DataSetExtensions
System.Web.Extensions
System.Web.Extensions.Design
System.Xml.Linq
System.Web.Abstractions
System.Web.Routing
System.Configuration
System.Web.Services
System.EnterpriseServices
Microsoft.Web.Infrastructure (1.0.0.0) *
Microsoft.Web.Mvc.FixedDisplayModes (1.0.0) *
Newtonsoft.Json (4.5.11) *
System.Net.Http (2.0.20710.0) *
System.Net.Http.Formatting (4.0.20710.0) *
System.Net.Http.WebRequest (2.0.20710.0) *
System.Web.Helpers (2.0.20710.0) *
System.Web.Http (4.0.20710.0) *
System.Web.Http.WebHost (4.0.20710.0) *
Remove the following project-wide Imports from the project settings:
System.Xml.Linq
System.Collections.Specialized
System.Configuration
System.Web.Caching
System.Web.Mvc.Ajax
System.Web.Routing
System.Web.SessionState
System.Web.Security
System.Web.Profile
System.Web.UI
System.Web.UI.WebControls
System.Web.UI.WebControls.WebParts
System.Web.UI.HtmlControls
Remove the following from packages.config:
<package id="Microsoft.AspNet.Mvc.FixedDisplayModes" version="1.0.0" targetFramework="net40" />
<package id="Microsoft.AspNet.WebApi" version="4.0.20710.0" targetFramework="net40" />
<package id="Microsoft.AspNet.WebApi.Client" version="4.0.20710.0" targetFramework="net40" />
<package id="Microsoft.AspNet.WebApi.Core" version="4.0.20710.0" targetFramework="net40" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="4.0.20710.0" targetFramework="net40" />
<package id="Microsoft.Net.Http" version="2.0.20710.0" targetFramework="net40" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net40" />
<package id="Newtonsoft.Json" version="4.5.11" targetFramework="net40" />
Delete the following sub-directories from the packages folder:
Microsoft.AspNet.Mvc.FixedDisplayModes.1.0.0
Microsoft.AspNet.WebApi.4.0.20710.0
Microsoft.AspNet.WebApi.Client.4.0.20710.0
Microsoft.AspNet.WebApi.Core.4.0.20710.0
Microsoft.AspNet.WebApi.WebHost.4.0.20710.0
Microsoft.Net.Http.2.0.20710.0
Microsoft.Web.Infrastructure.1.0.0.0
Newtonsoft.Json.4.5.11
This leaves me with the following:
A VB.Net class library project targeting .NET Framework 4 (although I had intended this to be 4.5 - I think either works).
The following non-default .NET references (starred references must have Copy Local and Specific Version set to True):
System.Web
packages\Microsoft.AspNet.Mvc.4.0.20710.0\lib\net40\System.Web.Mvc.dll *
packages\Microsoft.AspNet.Razor.2.0.20715.0\lib\net40\System.Web.Razor.dll *
packages\Microsoft.AspNet.WebPages.2.0.20710.0\lib\net40\System.Web.WebPages.dll *
packages\Microsoft.AspNet.WebPages.2.0.20710.0\lib\net40\System.Web.WebPages.Deployment.dll *
packages\Microsoft.AspNet.WebPages.2.0.20710.0\lib\net40\System.Web.WebPages.Razor.dll *
The following non-default imports:
System.Web
System.Web.Mvc
System.Web.Mvc.Html
The following project files/structure:
Controllers folder containing CustomUIController.vb
Models folder containing CustomUIModel.vb
Views folder containing CustomUI folder, containing Index.vbhtml
Web.config file in the Views folder. See below for content.
packages.config file in the root of the project. See below for content.
The contents of my files are as follows:
Web.config
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.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=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.Html" />
</namespaces>
</pages>
</system.web.webPages.razor>
</configuration>
packages.config
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.AspNet.Mvc" version="4.0.20710.0" targetFramework="net40" />
<package id="Microsoft.AspNet.Razor" version="2.0.20715.0" targetFramework="net40" />
<package id="Microsoft.AspNet.WebPages" version="2.0.20710.0" targetFramework="net40" />
</packages>
CustomUIController.vb
Imports System.Web.Mvc
Public Class CustomUIController
Inherits Controller
Public Function Index() As ActionResult
Return View()
End Function
End Class
CustomUIModel.vb
Public Class CustomUIModel
Public Property Name As String
Public Property Value As Decimal
End Class
Index.vbhtml
#ModelType CustomTemplate.CustomUIModel
#Html.LabelFor(Function(m) m.Name)
At this point I am able to work with Intellisense assisting me in the .vbhtml views and the .vb classes, build the project, then copy just the views to the primary application's deployed Views folder (in the appropriate sub-directory), and the project's primary output DLL to the primary application's deployed bin directory (the dependent DLL files are already there).
Edit:
After following the process on another system to validate it and how it works for .NET 4.5 and VS 2013, I have noticed the following:
I think I neglected to mention that the reference to System.Web.DynamicData can be removed.
In VS 2013 and/or .NET 4.5, some versions change:
packages\Microsoft.AspNet.Mvc.4.0.30506.0\lib\net40\System.Web.Mvc.dll
packages\Microsoft.AspNet.Razor.2.0.30506.0\lib\net40\System.Web.Razor.dll
packages\Microsoft.AspNet.WebPages.2.0.30506.0\lib\net40\System.Web.WebPages.dll
packages\Microsoft.AspNet.WebPages.2.0.30506.0\lib\net40\System.Web.WebPages.Deployment.dll
packages\Microsoft.AspNet.WebPages.2.0.30506.0\lib\net40\System.Web.WebPages.Razor.dll
I don't know if I made a strong enough point of it above, but the references to private (Copy Local) DLLs do have to be set to Copy Local, and/or do have to use the version provided in the packages folder when creating a project from the MVC4 template. I don't know why, but the .NET standard versions (non-private) don't seem to work, as far as Intellisense is concerned at least.
Best way to check if things are still "OK" as far as the IDE is concerned is to close the solution, delete the bin and obj folders in the custom project's output, re-load the solution, put the cursor on LabelFor in the Index.vbhtml file, and press the F12 key to see if it takes you to the Object Browser.
Because of the new versions, the packages file is different:
Packages.config
<packages>
<package id="Microsoft.AspNet.Mvc" version="4.0.30506.0" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="2.0.30506.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages" version="2.0.30506.0" targetFramework="net45" />
</packages>
Alright, this is a longshot, but I ran across the same problem. Are you using JetBrains Resharper?
Resharper overrides VS's intellisense engine but it doesn't understand Razor syntax. You simply have to tell VS to rely on its own intellisense rather than Resharper's.
In VS2010, go Resharper - Options - Intellisense - General. Then check the Visual Studio radio button.
Worked great for me.
None of the solutions I could find online or on SO fixed this for me.
May seem like a sledgehammer to crack a nut but I created an MVC 4 application project instead of a class library and ripped out everything I didn't need. Intellisense and #model working fine.

Resources