MVC 4 - Use Razor Views in Class Library - asp.net

I am trying to have MVC 4 Razor View in a Class Library Project.
I added Web.Config files to my class library root folder and views folder but in my view #model is still not recongnized.
I also added the Microsoft.AspNet.Mvc package to the project to.
Does anyone knows how to solve this?
This is my Web.Config files:
1 - 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=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:Enabled" value="false" />
</appSettings>
<system.web>
<httpHandlers>
<add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>
<!--
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=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>
2 - Root folder
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<appSettings>
<add key="webpages:Version" value="2.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>
<httpRuntime targetFramework="4.5" />
<compilation debug="true" targetFramework="4.5" />
<pages>
<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.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
</configuration>
Does anyone knows what am I missing?
Thank You,
Miguel

It turns out the easy solution is to create it as a web application, but that might not sit well with everyone.
First Attempt
I myself also had a class library I wanted to create some controls in, and yet I wanted to have a working Razor View, with full intellisence support. The trick to that is:
Add References (for MVC)
Add ProjectTypeGuids to .csproj (edit as text) after ProjectGUID: <ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
Add a .cshtml razor view file (it wont' work right until the next parts)
Add a Web.Config file (see your View folder web.config)
Change Project Properties Build output path to bin\ (not bin\Debug)
Clean and Build Solution
Gets You almost there (comments mention the Bin folder issue):
https://conficient.wordpress.com/2013/11/27/asp-net-razor-views-in-class-libraries/
Another Guide with Bin included:
http://thetoeb.de/2014/01/05/enabling-mvc5-intellisense-in-a-classlibrary-project/
But the only point to any of that is if you're avoiding having a real ASP.Net MVC site. So, now you'll need something to run it... and there are options, but that's beyond this entry.
Almost There
If you just want your views to be compiled, and re-used as a project by many other real ASP.Net MVC projects, then instead you could just Use Razor Generator , but even still here, you might not want to create your project as a Class library, because such just isn't fun...
Install VS Extension for Razor Generator
Add new Class Library Project
Add NuGet package to your library for RazorGenerator.Mvc
Add NuGet package to your library for PrecompiledMvcViewEngineContrib
Create a Views folder and a controller named folder under that in your library for the virual path you want these accessed as
Move any views to that new \Views\ControllerName\ folder
Set all these library views to Custom Tool RazorGenerator (right-click properties on view)
Add reference to your library from your real ASP.Net MVC web application
From there I often have problem still. I'll be stuck in C# 2.0 unless I add Microsoft.CodeDom.Providers.DotNetCompilerPlatform nuget package. Which is for 4.5, so for 4.6.x one could modify the new codedom section in app.config to
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701">
<providerOption name="CompilerVersion" value="v4.0"/>
</compiler>
<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>
Now you can use "var" as an implict type and other modern C# features.
Next I had an issue with System.Linq extension methods not being found despite having referenced it... and life just isn't quite right... if you want it all... But that is a start if that's a path you want to walk further down.
Simplest Option
But if you take the Advice of the Razor Generator crew, and actually install Razor Generator on top of a regular MVC Web Application (just create your "library" as one), then almost none of this work is necessary...
Have a Main real MVC ASP.Net site
Create a new MVC ASP.Net site to act as your "library", so you don't have to mess with things as much
Install Nuget Package for RazorGenerator.MVC to "library"
Install Nuget Package for PrecompiledMvcViewEngineContrib to "library"
Move views to similiar structure in library and mark as Custom Tool RazorGenerator
Reference your "library" from main site, and it'll all work with less hassle this way.
The beauty is that your referenced "library" has App Start code that will run at app start of your main app, and that will register the PreCompiledViewEngine, which knows about virual paths, and not just physical paths. Then your RazorGenerator Custom Tool views will define their virtual path, and when the view path is resolved, it can find them.
Nice Reference on the topic:
https://www.c-sharpcorner.com/UploadFile/97fc7a/article-precompiled-razor-view-using-razorgenerator-mvc-and/

Your project must be a Web Application for intellisense to work properly. Web Application is just a special class library.

Related

The name 'ViewBag' does not exist in the current context mvc 5

I have web.config inside 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.2.2.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="PartyInvites" />
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
</appSettings>
<system.webServer>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</configuration>
I also make sure the solutions Web.config is updated
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-5.2.2.0" newVersion="5.2.2.0"/>
</dependentAssembly>
But when i look at the views, i still get the error
Compiler Error Message: CS0103: The name 'Viewbag' does not exist in
the current context
Looks like my issue is an isolated case, VS tells me it get screwed after i updated some extension, i cannot remember what extension made it like this so i just repaired the installation of VS and it worked.
I had the same issue on Visual Studio 2015. It was resolved by updating the .Net Framework to use 4.5.2 instead of 4.0. This is done in the Properties page of the project.
I had this issue regardless of having all the correct configuration done in the web.config file.
Found out to be some bad files in the Component Cache, preventing the Razor views from recognising ViewBag, Model, and HtmlHelpers. Deleting these files solved the problem (good versions of these files were created next time I opened Visual Studio).
Please follow below path to discover the files:
C:\Users\your.name.here\AppData\Local\Microsoft\VisualStudio\14.0\ComponentModelCache
Delete all four files:
Microsoft.VisualStudio.Default.cache
Microsoft.VisualStudio.Default.catalogs
Microsoft.VisualStudio.Default.err
Microsoft.VisualStudio.Default.external
I closed my project, deleted the files on that path and reopened my project, cleaned the solution and built it again and the problem was solved
Deleting your Temporary ASP.NET Files also helps. C:\Users\your.name.here\AppData\Local\Temp\Temporary ASP.NET Files.
Thanks!

Upgrading MVC-3 to MVC-4

Before posting this, I did read here and SO question -
I took a little different approach, which seems easier.
Since MVC-3 application contained very few controllers and views. I created a new MVC-4 - Basic application and copied following files -
Controllers
Views
Layout files
Routing info from old Global.asax.cs to App_Start/Route.Config
The advantage I see with this approach is
There was no need to refer new Dlls manually.
There was no need to update Web.config references.
Build goes fine, however while running the application I end up with following error.
[A]System.Web.WebPages.Razor.Configuration.HostSection cannot be cast
to [B]System.Web.WebPages.Razor.Configuration.HostSection.
Type A
originates from 'System.Web.WebPages.Razor, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context
'Default' at location
'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_1.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'.
Type B originates from 'System.Web.WebPages.Razor, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context
'Default' at location
'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'.
Obviously somewhere MVC3 dlls are being referred. But not sure where? Any advice on this would be helpful. Thanks !
If I had to guess, I'd say your ~/Views/web.config was overwritten and still is pointing to the older razor parser (and subsequently the older config section). Try replacing that file with the following:
<?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.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>
<!--
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=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>

Images folder in separate applications

I have a web application with an admin panel. The admin panel is a webform project and I also have an MVC project to list products inserted in admin panel. But when I upload images to ProductImgs folder in webform project, how can I get them from MVC project? Or can I save them to the MVC project folder?
Ok so sounds like you are trying to share images between two web applications? EDIT Also you are trying to do this using Cassini (the inbuilt dev web server).
The easiest way to do this is to move your development environment to using IIS and then create a virtual directory within your mvc project in IIS that points to the images folder in your other site (on your file system). NB Moving to IIS will also have the added benefits of making your solution be more in line with how it will be deployed which IMHO is a great benefit.
Say you call this virtual directory 'images' and it has two images (image1.jpg and image2.jpg).
You can then reference these images from your mvc site by using
<img src="/images/image1.jpg" />
Here is a link on how to create Virtual Directories.
http://support.microsoft.com/kb/172138
Now i found the solution: here is the root web.config to solve this issue
<location inheritInChildApplications="false">
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Helpers, Version=1.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=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>
<pages>
<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"/>
<add namespace="EkosWeb.Helpers"/>
</namespaces>
</pages>
</system.web>
</location>
I need to add inheritInChildApplications="false" to location.

Problem with Elmah in the GAC

I am attempting to follow this article:
http://www.codeproject.com/KB/aspnet/elmahGAC.aspx
to get Elmah working from the GAC rather than needing to be setup for each application individually. Everything works locally but when the settings are set globally, Elmah stops logging. I read somewhere that if Elmah is running from the GAC then the settings should be in the "global" machine.config vs the "global" web.config but I've tried both. Right now, I'm at a point where if I add this:
<system.webServer>
<modules>
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah, Version=1.2.13605.0, Culture=neutral, PublicKeyToken=abc123" preCondition="managedHandler" />
</modules>
</system.webServer>
into my application's web.config, it picks up the rest of the settings from the global machine.config and logs successfully. To answer the obvious question, yes I have this section in the global machine.config and have even tried entering it in the global web.config but still logging won't work. Anyone have any ideas? Is there anyway to get Elmah to display its errors instead of failing quietly?
EDIT: Here is my "global" machine.config file. It's just the default one with the stuff for ELMAH added and some section groups taken out to meet the char limit. I should probably also note this is running in IIS7.
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<configSections>
<sectionGroup name="elmah">
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah, Version=1.2.13605.0, Culture=neutral, PublicKeyToken=e50dbdd41da277ac" />
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah, Version=1.2.13605.0, Culture=neutral, PublicKeyToken=e50dbdd41da277ac" />
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah, Version=1.2.13605.0, Culture=neutral, PublicKeyToken=e50dbdd41da277ac" />
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah, Version=1.2.13605.0, Culture=neutral, PublicKeyToken=e50dbdd41da277ac" />
</sectionGroup>
</configSections>
<connectionStrings>
<clear />
<add name="Elmah.Sql" connectionString="IMNOTTELLING" providerName="System.Data.SqlClient" />
<add name="LocalSqlServer" connectionString="IMNOTTELLING" providerName="System.Data.SqlClient" />
</connectionStrings>
<elmah>
<security allowRemoteAccess="0" />
<errorLog type="Elmah.SqlErrorLog, Elmah, Version=1.2.13605.0, Culture=neutral, PublicKeyToken=e50dbdd41da277ac" connectionStringName="Elmah.Sql" />
</elmah>
<runtime />
<system.data>
<DbProviderFactories />
</system.data>
<system.serviceModel>
Nothing tampered with here
</system.serviceModel>
<system.web>
<processModel autoConfig="true"/>
<httpHandlers>
<add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah, Version=1.2.13605.0, Culture=neutral, PublicKeyToken=e50dbdd41da277ac" />
</httpHandlers>
<httpModules>
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah, Version=1.2.13605.0, Culture=neutral, PublicKeyToken=e50dbdd41da277ac" />
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah, Version=1.2.13605.0, Culture=neutral, PublicKeyToken=e50dbdd41da277ac" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah, Version=1.2.13605.0, Culture=neutral, PublicKeyToken=e50dbdd41da277ac" />
</httpModules>
<compilation>
<assemblies>
<add assembly="Elmah, Version=1.2.13605.0, Culture=neutral, PublicKeyToken=e50dbdd41da277ac" />
</assemblies>
</compilation>
<membership>
Nothing tampered with here
</membership>
<profile>
<providers>
<add name="AspNetSqlProfileProvider" connectionStringName="LocalSqlServer" applicationName="/"
type="System.Web.Profile.SqlProfileProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
</profile>
<roleManager>
Nothing tampered with here
</roleManager>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests ="true">
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah, Version=1.2.13605.0, Culture=neutral, PublicKeyToken=e50dbdd41da277ac" preCondition="managedHandler" />
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah, Version=1.2.13605.0, Culture=neutral, PublicKeyToken=e50dbdd41da277ac" preCondition="managedHandler" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah, Version=1.2.13605.0, Culture=neutral, PublicKeyToken=e50dbdd41da277ac" preCondition="managedHandler" />
</modules>
<handlers>
<add name="Elmah" path="elmah.axd" verb="POST,GET,HEAD" type="Elmah.ErrorLogPageFactory, Elmah, Version=1.2.13605.0, Culture=neutral, PublicKeyToken=e50dbdd41da277ac" preCondition="integratedMode" />
</handlers>
</system.webServer>
</configuration>
EDIT: I installed the modules and handler in IIS directly and now it works.
I also had problems with IIS7 on a Win7x64 install (testing for a production system). I finally got this working by putting the module and handler into the appropriate nodes in the applicationHost.config file.
C:\Windows\System32\inetsrv\config\applicationHost.config
It seems that IIS7 ignores certain areas of the main framework web.config files, instead using the applicationHost.config file for IIS settings.
Something to note about that file if you are on a 64 bit install: it seems that 32 bit applications (including Visual Studio) will appear to not see the file. It's because of some file system behavior due to the OS being 64 bit. One easy work around is to open it using an UNC path:
\\localhost\c$\Windows\System32\inetsrv\config\applicationHost.config
I really doubt the PublicKeyToken of Elmah is abc123... You need to use the real PublicKeyToken, which you can find if you look at the assembly in the GAC. It looks something like: b03f5f7f11d50a3a (that was for mscorlib.dll, so that is Microsoft's public key token).
EDIT: I installed the modules and handler in IIS directly and now it works.
I've tried everything I could find to get ELMAH working from the GAC on my Win7 Ultimate 64bit machine and the only thing that finally got it working was registering the elmah.axd handler and the various modules in IIS Manager.

"The name 'HTML' does not exist in the current context" in MVC 3 Views

I´m starting to use "MVC 3" but I´m facing some little problems. In my Views, when I code something like this:
#if(Request.IsAuthenticated) {
<text>Welcome <b>#Context.User.Identity.Name</b>!
[ #Html.ActionLink("Log Off", "LogOff", "Account") ]</text>
}
else {
#:[ #Html.ActionLink("Log On", "LogOn", "Account") ]
}
The objects like #Request and #Html is indicating an error: The name 'HTML' does not exist in the current context.
The same occurs with #Context, #ViewBag, #Layout, #Url and others.
See:
But the code is correctly compiled with no errors. The problem is that I cannot use the Intellisense with theses objects in the Views. Is it normal? (I don´t think so). What could be happening?
I have reinstalled the MVC 3 framework but the same still occurs.
Note: this is a new project from scratch, not a MVC 2 migration. This occurs both with Razor engine and ASPX.
This is the Web.Config in 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=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>
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
<system.web>
<httpHandlers>
<add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>
<!--
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=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>
</configuration>
Thanks!
You could try:
Close the View with the false errors.
(Steps 2-5 are sometimes optional) Close Visual Studio
Delete the bin and obj folders in all the Projects in the Solution.
Reopen the same Solution
Rebuild the Solution
Open a different View then the one causing the errors
Close that View, hopefully you didn't see any of the similar errors in this View
Reopen the View that gave you problems earlier
I have solved this issue with the old, good, wise Microsoft default solution: reinstall all the things again.
Uninstall and Reinstall the Visual Studio 2010 and MVC 3 Framework.
Clean your solution and Under references.
Then set the follow property:
System.Web.MVC file to Copy Local = True.
All I had to do was close all views that were open in the editor and rebuild.
What worked for me was closing Visual Studio, deleting the user option files (both solution and project level), then relaunching Visual Studio.
I'm using dotnet core sdk 2.2 and Visual Studio Professional 2019.
This was caused by Visual Studio mangling code.
Code was pasted into the razor view. Visual Studio would attempt but then fail to auto-format it. Part of the failure resulted in deleting code.
Pressing ctrl+z reverted the formatting but kept the pasted code. The razor compilation errors were then fixed (The name 'Html' does not exist in the current context)
Disable format on paste (see https://stackoverflow.com/a/28053865/1462295 ) under Tools -> Options -> Text Editor -> HTML -> Advanced
For me this just seemed to be the fact that I had compiler warnings. Code would still compile and run ok but it was not until I fixed all the build warnings that my Intellisense starting working.
I tried to remove the project that is still in trouble from solution and add it back again, after which the problem had been gone.
Set the property of System.Web.MVC,Copy Local = True
In my case my Packages folder was missing including MVC and Razor so I have updated packages in packages.config, reopened the view and it worked.
All you need to do is Restart Visual Studio and check.
You could try some following point:
Close the View with the false errors.
Close visual studio.
open project again.
clean your solution
Rebuild your solution.
hopefully you will not get any of the similar errors now.
If you still have same problem. than Please check following steps.
Make sure you have web.config file inside view folder. Becuase MVC has two web.config. one for solution and other one for project.
Your web.config file will seems like this which is inside 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.2.7.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="Your Project Name" />
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
<system.webServer>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
<system.web>
<compilation>
<assemblies>
<add assembly="System.Web.Mvc, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
</system.web>
</configuration>
It worked for me. and hope for you also.
Close Visual Studio => Delete the obj and bin folders located in your project => Start Visual Studio
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
solution for this problem in web.config
I experienced this on ASP.NET MVC 4 as well, after uninstalling EntityFramework from my packages list.
I had to remove this section
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, ....
that's left on the Web.config file... at the least it removed the errors from the errors list
Apologies for the necro post.
Selecting "Build | Rebuild Solution" corrected this issue for me in Visual Studio 2015. In my case, the warnings occurred after renaming the primary namespace of a project. A rebuild set everything straight.
I'm using ASP .net core. Solved mine by upgrading Microsoft.AspNetCore.Mvc from 1.1.2 to 1.1.3.
For me, I just restarted my Visual Studios and everything got fixed.
Maybe I am a bit late to answer this question but this easy fix helped me:
Right click the file > Exclude from project.
Right click the file > Include in project.
I had this same issue in MVC 4. None of these solutions worked for me. Instead, in Windows, I went into Control Panel -> Uninstall a program. Select Microsoft ASP.NET MVC 4 in the program list. Click 'Uninstall.' A 'Microsoft ASP.NET MVC 4 Setup' wizard will display. Click 'Repair.'

Resources