VS 2017 Intellisense Not Working without Views Web.Config - c#

I work on a large Visual Studio Solution (Sitecore Helix), with separate "feature" Projects. Some Projects have a Views > Web.config file and others do not.
During our build process, all Views end up one location/project and in that final destination the Views folder does have it's production Web.config file.
However, without a Views > Web.config file at individual the Project, there is no IntelliSense for the (dynamic) #model. Strongly typed Objects (#Html.Raw) have autocomplete/IntelliSense. As a front-end person, I really rely on the IntelliSense.
Is there a better/more appropriate way of adding these references other than to adding an individual Web.config (example below) to each Project, especially since I just need this for debug/development.
Example Web.config, please note this is not my entire Web.config file, but specifically the lines I have narrowed down to make the IntelliSense work in this case.
<?xml version="1.0" encoding="utf-8"?>
<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="Sitecore.Mvc" />
<add namespace="Sitecore.Mvc.Presentation" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="GCC.Feature.WebAdmin" />
</namespaces>
</pages>
</system.web.webPages.razor>
</configuration>
IntelliSense working on something other than #model
IntelliSense not working on #model no web config
IntelliSense working with Web.config

Related

Web.config in Views subfolder ignored on developer machine, why?

I have an ASP.NET MVC application with a root web.config and a secondary Views/web.config added automatically by Visual Studio years ago when the application was created.
Now I have bumped ASP.NET MVC to a newer version (5.2.7), fixed various issues on my developer machine - and everything works for me.
Unfortunately, when pushed to our integration test server, everything fails as there are MVC version references, in the Views/web.config file, that are now invalid for the new MVC version.
I can fix the version issues and everything is 200 OK ... but, and here is my question, why doesn't the errors show up on my local developer machine?
It turns out I can introduce XML syntax errors and what not on my local machine - and nothing happens, everything is okay locally, as if the Views/web.config file is completely ignored by the webserver.
Both machines are running IIS on Windows with the same configuration ... well, except that something apparently is different.
Where should I look to figure out why the Views/web.config is ignored on my developer machine?
The Views/web.config looks like this (with the wrong version numbers included):
<?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.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="CBrain.F2.SelfService.Mvc.Html" />
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
<system.web>
<compilation>
<assemblies>
<add assembly="System.Web.Mvc, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
</system.web>
<system.webServer>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</configuration>
Turns out that IIS behaves a bit strange: once the webpage loads correctly first time, I can change the Views/web.config without IIS detecting the change. But then I modify the main web.config ... whereafter IIS detects my changes in Views/web.config. Guess I have to live with that.

The name 'Model' does not exist in the current context ASP.NET MVC5

I am coding a PlugIn to NopCommerce 3.2, and in the views I get the error mentioned above, but also for 'Layout' and 'Html'. I have copied the same Web.Config that works in another plugin. I have also added the same references as used in the working Plugin. When compiling the plugin works, but I don't have any intellisense and can't trust that VisualStudio marks errors correctly. I am using VS2013 Professional Update 1.
Here is the Web.Config file:
<?xml version="1.0" encoding="utf-8"?>
<!-- We use this file to make razor intellisense work in the class library -->
<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>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<compilation targetFramework="4.5" />
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
<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.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
</system.web>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="Nop.Web.Framework.ViewEngines.Razor.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>
</configuration>
EDIT: Sorry if you find this question not researched, but I assure you I have spent hours to try to find the solution (including those questions I have found here in stackoverflow that are similar and pointing out web.config as the problem which is the reason I have included that file in my question) but can't find it.
This error was probably caused by Resharper. We are a team of three developers using GIT. One of us installed Resharper, and this probably caused the error for the other two of us. When we installed Resharper trial it suddenly worked again. Unfortunately it isn't working when uninstalling Resharper again...
Try to add this section in system.web. (Make sure the versions i added are indeed the ones you are using)
<compilation targetFramework="4.5">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Helpers, 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.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.WebPages, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
Close the .cshtml view files. Rebuild the project. And open them again.
Un-installing the project MVC Nuget Package and Reinstalling it fixed it for me. Mine got botched when I changed the .net version of the project.
Ensure that the namespace that you have defined on view is matching with your model's namespace.
If you change your project properties to target 4.5.1,
targetFramework in LOTS of other places will still say 45
You can find targetFramework 3-4 times in your root web.config
and in all kinds of packages.config.
I had same problem, change Project to target 4.5 and it cleared up.
Best to not target 4.5.1
I had the same problem. The solution was to uninstall the project MVC Nuget Package and Reinstall it.
http://mhammadchehab.com/wordpress/2013/12/enabling-intellisense-for-razor-in-class-library-mvc-5-razor-3-0/
Change the Project Output path to /bin for All Configurations in the Project Properties. This is odd, but it seems when the VS Project Output is pushed to /bin/Debug or /bin/Release the intellisense is completely busted.
I experienced the same error in my MVC 5 project. My case was that the Web.config in Views folder was not published somehow. Adding it back solved the issue.
I my case, I had to reset the Visual Studio Component Cache by closing Visual Studio and deleting this folder:
C:\Users\[username]\AppData\Local\Microsoft\VisualStudio\14.0\ComponentModelCache
I had the same error and just changed
<pages pageBaseType="Nop.Web.Framework.ViewEngines.Razor.WebViewPage">
to
<pages pageBaseType="System.Web.Mvc.WebViewPage">
I am just trying to get cshtml intellisense in a console application so it worked for me to go back to the standard base webviewpage.

Razor highlight stopped working

I have modular MVC Portal-Plugin application. When I've changed output path of mvc application Razor highlight and intellisense stop to work in cshtml files. Can anybody faced with this issue before? I've also added post build event to copy Views into custom directory
I have main "Container" MVC application and modules (mvc projects)
Main mvc application and modules reside in one solution
There is next options on each module MVC application:
Output Path : ..\Container\bin\
Post build Event: xcopy "$(ProjectDir)\Views" "..\Modules\$(ProjectName)\Views" /s /i /y
After this manipulations my Razor intelisense and highlight stop to work in module application.
I solve my problem. It was problem with output path.
I've just put default output path "bin\" and add post build event to copy my dll from this folder into another one.
Solved it and blogged about it in here...
http://mhammadchehab.com/wordpress/2013/12/enabling-intellisense-for-razor-in-class-library-mvc-5-razor-3-0/
*Right Click – >Manage Nuget Packages
*Click on the installed Packages.
*Click Uninstall.
*When your done search for MVC 5 online and install the Library.
*Right click on your project and go to properties
*Select Build
*On Configuration select all configuration.
*Go to the output path and type “bin\”
*Clean your project and rebuild.
*Make sure that your cshtml file is closed.
*Reopen your view file and BOOOOOOM it works.
Make sure your web.config file in your Visual studio project contain the following lines:
<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>
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="MyCustomHelpers" />
</namespaces>
</pages>
</system.web.webPages.razor>\
These lines give you the intelli sense.
Changing the Output Path to bin/ is the correct answere, but I extended it a bit. the steps needed for all creating a "vanilla" class library with intellisense support are the following
Create or open an existing class library project (if you open an existing one be sure to remove the MVC5 nuget package)
Add the MVC (5.0) nuget package (right click project in solution explorer -> Manage NuGet Packages -> search for MVC and install “Microsoft ASP.NET MVC”)
Close any and all open .cshtml files
Right click project -> Properties -> Build -> change Output path to “bin/”
Add the following minimal Web.config to the root of your class library project ( the web
config file is solely needed for intellisense. Configuration (via Web.config) should be done in the WebApplication hosting your ClassLibrary assembly)
Clean and Build the solution.
Open cshtml file
web.config:
<?xml version="1.0" encoding="utf-8"?>
<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>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
</system.web>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.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" />
<!-- add other namespaces for views here -->
<!-- e.g. your own project's, Lib.Views.Etc -->
</namespaces>
</pages>
</system.web.webPages.razor>
</configuration>
I also just blogged about this http://thetoeb.wordpress.com/2014/01/05/enabling-mvc5-intellisense-in-a-classlibrary-project/
I resolved it as follows. In web.Config, add this:
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
</appSettings>
For reference, webpages:Version does this:
If the key is missing from web.config file such as default Razor V2/V3
website, VS will use bin directory’s razor dll version to determine
the design time razor engine dll to load.
https://stackoverflow.com/a/27918972/3146

Razor Views not seeing System.Web.Mvc.HtmlHelper

I am in the process of upgrading to MVC4. I have followed the instructions at http://www.asp.net/whitepapers/mvc4-release-notes#_Toc303253806 but in my Razor views and layouts I have errors like
'System.Web.WebPages.Html.HtmlHelper' does not contain a definition
for 'BeginRouteForm' and no extension method 'BeginRouteForm'
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 also have errors like:
"The name 'Viewbag' does not exist in the current context
When I hover over #Html I can see it is of type System.Web.WebPages.Html.HtmlHelper not System.Web.Mvc.HtmlHelper
Not really sure what other information is pertinent but this is where I am:
I haven't updated class librarys etc.. in the sln to .net 4.5 yet.
The project was created in VS2010, but I am doing the migration in
VS2012
The project opens and runs OK with VS2012, .Net 4.0, MVC 3
Any pointers appreciated.
Edit:
All references and files in web.config upgraded to:
System.Web.Mvc, Version=4.0.0.0
System.Web.WebPages, Version=2.0.0.0
System.Web.Helpers, Version=2.0.0.0
System.Web.WebPages.Razor,
Version=2.0.0.0
Edit(2):
In my /views/web.config (or /views/shared/web.config if I try #Paul 's solution below) in the element, I have my own base type which inherits from System.Web.Mvc.WebViewPage , the references in the containing library have been updated to MVC4 and go to definition takes me to the MVC4 dll.
Has anything changed in this area on the new release? I couldn't see anything pertinent in the release notes.
I ran into this issue with a Web Application - my .cshtml files got stuck on the System.Web.WebPages.WebViewPage base class when I needed the System.Web.Mvc.WebViewPage.
First, ensure your ~/Views/web.config file has the correct pageBaseType. In my case, I set to System.Web.Mvc.WebViewPage.
<configuration>
<system.web.webPages.razor>
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<!-- ... -->
</pages>
</system.web.webPages.razor>
</configuration>
Then, importantly, some people have found this is key if the above is already good:
Run a clean on the solution
Unload the project with the issues
Delete the .user file that Visual Studio generated next to the project.
Reload the project with the issues
Build the solution
For VS2015, the .user and .sln files have moved to the .vs hidden folder that is created next to the .sln file. However, from comments below, the error messages imply to me that the tooling is using the wrong version of MVC entirely, and deleting this folder does not fix the issue. To my knowledge, there is not a known solution.
I had the same issue when updating to MVC 5 and it was solved by updating the web.config inside the Views folder.
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.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>
The host -> factoryType was set to version:4.0.0.0
hope this helps anyone.
I tried all the solutions here but none of them worked for me. Again, my site runs fine but I don't have intellisense and get red wavy lines under a lot of things in my views that Visual Studio does not recognize, one of them being Html.BeginForm(), as well as anything having to do with ViewBag.
I'm working with a new MVC 5 project. After hours of comparing web.config lines, I finally found what fixed it for me.
My web.config in my root had the following line:
<system.web>
<compilation debug="true" targetFramework="4.5" />
<!-- ... -->
</system.web>
I compared to a previous project not using MVC 5, and copied over a block I noticed was missing from the new one, which was the following:
<system.web>
<compilation debug="true" targetFramework="4.5">
<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.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.WebPages, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
<!-- ... -->
</system.web>
I copied the above block over to my new project's web.config in the root, changing the versions to match the numbers for each assembly found in my project references (right-clicking each reference mentioned and selecting "Properties", "Version" is given at the bottom of the properties window for the selected reference).
After implementing the above, I now have intellisense and don't get any unknown red lines under things like Html.BeginForm, ViewBag.Title, etc.
I had run a project clean, and installed or reinstalled everything and was still getting lots of Intellisense errors, even though my site was compiling and running fine. Intellisense finally worked for me when I changed the version numbers in my web.config file in the Views folder. In my case I'm coding a module in Orchard, which runs in an MVC area, but I think this will help anyone using the latest release of MVC. Here is my web.config from the Views folder
<?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="Orchard.Mvc.ViewEngines.Razor.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.Linq" />
<add namespace="System.Collections.Generic" />
</namespaces>
</pages>
</system.web.webPages.razor>
<system.web>
<!--
Enabling request validation in view pages would cause validation to occur
after the input has already been processed by the controller. By default
MVC performs request validation before a controller processes the input.
To change this behavior apply the ValidateInputAttribute to a
controller or action.
-->
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<controls>
<add assembly="System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" 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>
You need to copy Views/Web.config to /Shared. This will tell Razor to use the MVC base type & parser. You can read more here: http://blog.slaks.net/2011/02/dissecting-razor-part-3-razor-and-mvc.html
I was dealing with this issue after upgrading from Visual Studio 2013 to Visual Studio 2015 After trying most of the advice found in this and other similar SO posts, I finally found the problem. The first part of the fix was to update all of my NuGet stuff to the latest version (you might need to do this in VS13 if you are experiencing the Nuget bug) after, I had to, as you may need to, fix the versions listed in the Views Web.config. This includes:
Fix MVC versions and its child libraries to the new version (expand the References then right click onSytem.Web.MVC then Properties to get your version)
Fix the Razor version.
Mine looked like this:
<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: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.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=5.2.3.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>
Update for Visual Studio 2017 Users:
If you have just migrated to Visual Studio 2017 and your project is MVC4, make sure you go back into the VS2017 Installer and check the MVC4 option under the "ASP.NET and web development" section. It is an optional component and not checked by default.
This solved my issue
In the contracting world I'm often using machines that are using older images. After trying everything above I decided to update my VS 2013 to the most recent version (Update 4). After 90 minutes and a restart the references are working just fine now! Hope this helps!
Having tried everything in vain, I discovered that in my case it wasn't working because an incorrect attribute value in Web Project csproj file. When I change ToolsVersion to 14, which matches my current IDE version (i.e. Visual Studio 2015), everything worked like a charm:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition=
.....
For those of you suffering with this after migrating a project from VS 2013 to VS 2015, I was able to fix this issue by installing the ASP.NET tools update from https://visualstudiogallery.msdn.microsoft.com/c94a02e9-f2e9-4bad-a952-a63a967e3935/file/77371/6/AspNet5.ENU.RC1_Update1.exe?SRC=VSIDE&UPDATE=TRUE.
Just to expand on Matt DeKrey's answer, just deleting the csproj.user file (without needing to recreate solutions) was able to fix the problem for me.
The only side effect I had was I needed to reset the Start Action back to using a specific page.
I used the following article on MSDN to solve this issue (in this case from MVC 4 to MVC 5)
https://learn.microsoft.com/en-us/aspnet/mvc/overview/releases/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2
Main extracts from the article are:
Main Web.config (Not the one in the Views folder)
In the runtime block update System.Web.Mvc to 5.0.0.0 ,System.Web.Helpers to 3.0.0.0 and System.Web.WebPages to 3.0.0.0
<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.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.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>
Then look for the appsettings block and update the webpages:Version value to 3.0.0.0
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
</appSettings>
Views Web.config
Update the host factoryType block to System.Web.Mvc to 5.0.0.0
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
</namespaces>
</pages>
</system.web.webPages.razor>
Then under the pages block update any references to System.Web.Mvc to 5.0.0.0
<pages
      validateRequest="false"
      pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
      pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
      userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
    <controls>
      <add assembly="System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
    </controls>
  </pages>
And finally under the configuration block update the System.Web references to 3.0.0.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>
Recently got the same problem and this is how i fixed it:
On Visual Studio with your project in question open, Goto:
Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution.
On the open window, select Updates. then Click Update All.
It will load whats missing in your project and all should be back on track.
Just started looking into the issue myself and this is what it look like in my case. If you have the correct values in your web config then Its just a bug in MVC4.
http://connect.microsoft.com/VisualStudio/feedback/details/727729/viewbag-not-recognized-in-asp-net-mvc-4-project
*<system.web>
<compilation debug="true" targetFramework="4.5">
<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.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.WebPages, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>*
This configuration is missing, add it and set appropriate version of assemblies
As a variation on a theme, I could have sworn up and down that my Views\Web.config was correct:
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
But I in fact needed to reference Version=4.0.0.1, not Version=4.0.0.0 because of that security update that got pushed out a while back.
As for me, it was a stupid deployment mistake: Web projects can have more than one web.config. It was working on the developer's machine and not in production, but we were not realising that the deployment script only grabbed the Web.config file at the root, and it didn't copy the Web.config file in the Views folder.
I've encountered this specific issue as well; no (or incorrect) intellisense showing when trying to use razor tags. My specific issue was VS2015 complaining that Html.BeginForm was not existing in the current context.
I've got areas set up in my MVC project, and managed to narrow down the cause of my error to the web.config file for a specific area, rather than the global web.config.
It turns out that the cause of this issue for me was that I'd added a SQL connectionString into the web.config for the area that was not working, this (I am assuming) caused a parsing error, however the project compiled up correctly.
Moving the connectionString to the global web.config has rectified the issue. Hopefully this may be of some use to others.
My situation only occurred after I created a custom class called BaseViewPage that overrode the WebViewPage class. I initially added the following to my Main Web.confg file:
<pages pageBaseType="ZooResourceLibrary.Web.Support.BaseViewPage">
And the same to the View folders web.config file:
<pages pageBaseType="ZooResourceLibrary.Web.Support.BaseViewPage">
I tried many of the other answers and none did the trick while still allowing me to keep my BaseViewPage class. The way I fixed it was to remove the pageBaseType attribute from the Main Web.config file only. Keep it in the View web.config.
I came across several answers in SO and at the end I realized that my error was that I had misspelled "Html.TextBoxFor." In my case what I wrote was "Html.TextboxFor." I did not uppercase the B in TextBoxFor. Fixed that and voilà. Problem solved. I hope this helps someone.
Right, I've been trying to fix this issue for sometime. I've used all the solutions in the various Stack Overflow topics regarding this and none seemed to be working.
I have just fixed the issue this morning. After you gave fixed the web.config for both the project and the views, making sure all the .dll versions are matching with what you have in the references folder. You will need to unload the project, edit the .csproj, and then update all the .dll versions in that file.
System.Web.Helpers
System.Web.Mvc
System.Web.WebPages
Hope this helps, as I have finally fixed this issue! No more red squiggly lines.
This has also fixed the context menu issue I was having where, I wasn't getting the option to add a controller, view etc.
For me the solution was to change the following:
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>

Get razor intellisense in library project?

I have a business project where I will be creating emails based on templates made with razor. How can I get razor intellisense there? Is it as simple as including a few assemblies or do I need to do something more retorted?
This is for using the RazorEngine library.
You have to edit your .csproj file. Add following ProjectTypeGuids node (add just bellow them bellow existing ProjectGuid node).
<ProjectGuid>{28AD1627-3486-48C2-A045-EFFBB441582B}</ProjectGuid>
<ProjectTypeGuids>{E3E379DF-F4C6-4180-9B81-6769533ABE47};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
Save file, then reopen it with Visual Studio. VS2012 then performs some conversion, but at the end everything is OK. Tooling is there.
Depending what you are doing (Razor Generator?) you will need some references (System.Web, System.Web.WebPages, System.Web.Mvc, System.Web.Razor, System.Web.Routing...).
This was tested with VS2012.
In Visual Studio 2013 I had to also replace the following line in the .csproj file because it pointed to a wrong loaction:
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
This is a nice guide to achieve this.
http://thetoeb.de/2014/01/05/enabling-mvc5-intellisense-in-a-classlibrary-project/
The steps are:
Add the MVC (5.0) nuget package (right click project in solution explorer -> Manage NuGet Packages -> search for MVC and install “Microsoft ASP.NET MVC”)
Close any and all open .cshtml files
Right click project -> Properties -> Build -> change Output path to “bin/”
Add the following minimal Web.config to the root of your class library project (the web config file is solely needed for intellisense. Configuration (via Web.config) should be done in the WebApplication hosting your ClassLibrary assembly)
Clean and Build the solution.
Web.config:
<?xml version="1.0" encoding="utf-8"?>
<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>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
</system.web>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.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" />
<!-- add other namespaces for views here -->
<!-- e.g. your own project's, Lib.Views.Etc -->
</namespaces>
</pages>
</system.web.webPages.razor>
</configuration>

Categories