Does the razor view autocomplete need to work in the class library?

We have a modular architecture in which we have several view files (cshtml) in a separate project (class library). How to make syntax highlighting work and autofill if the project is not an MVC project?

Note that the class library has controllers, views, models, etc. It simply does not have web.config, global.asax, etc., which a regular mvc project would have.

Intellisense works for everything except this important model: screenshot of model error

With MVC3 RTM, if you hover over the Model, you may get a better error message:

C: \ ... \ Index.cshtml: ASP.NET runtime error: no assembly provider registered to extend '.Cshtml. You can register one in & Lt; compilation> & l; buildProviders> section in machine.config or web.config. Make sure that there is a BuildProviderAppliesToAttribute attribute that includes the value "Network" 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 assembly 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 specified file. (C: \ ... \ machine.config line 259)

+47
autocomplete asp.net-mvc asp.net-mvc-3 razor intellisense
Nov 11 '10 at 19:42
source share
5 answers

Webconfig from this post will work. I 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> 
+49
Jan 20 2018-11-22T00:
source share

For Visual Studio 2012 / ASP.NET MVC 4, you need to update the build versions and add <add key="webpages:Version" value="2.0.0.0" /> to appSettings . This is 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> 
+12
Jun 14 '13 at 16:14
source share

I followed Jammer's suggestion and documented, as far as I think, the minimum set of steps to get a project suitable for working as an ASP.NET MVC4 class library project. This was done in Visual Studio 2012 Update 4 and was intended for VB.Net. Subsequently, I can include documentation in Visual Studio 2013 to do something similar if I get a chance. Here are the steps I took:

  • Create a new empty MVC4 project with a separate directory for the solution (so you can clearly see that the packages directory with updated versions of the referenced files is part of the project).
  • Add a dummy controller, view, and model to test it (which led to copying the output DLL and viewing another web application after creating the class library).
  • Delete the following files from the project:
    • Entire App_Start Folder
    • The entire App_Data folder and any other empty folders (my Mercurial history did not make this visible, so I'm moving from memory).
    • Global.asax
    • Global.asax.vb
    • The Web.config file in the root and 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 can also delete the lines <add namespace="System.Web.Mvc.Ajax" /> and <add namespace="System.Web.Routing" />
  • Remove the following links from the project (* the marked links were version links that were included in the packages directory and have Copy Local and Specific Version for 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 ( Newtonsoft.Json ) *
    • 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 imports from the project settings for the entire project:
    • 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" />
  • Remove the following subdirectories from the package 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:

  • The VB.Net class library project focused on the .NET Framework 4 (although I assumed that it would be 4.5 - I think it either works).
  • The following non-.NET links (marked links must have Copy Local and Specific Version for 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 imports are not default:
    • System.web
    • System.Web.Mvc
    • System.Web.Mvc.Html
  • The following files / project structure:
    • Controllers folder containing CustomUIController.vb
    • Models folder containing CustomUIModel.vb
    • Views containing the CustomUI folder containing Index.vbhtml
  • Web.config 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 can work with Intellisense, helping me in .vbhtml views and .vb classes, creating a project, and then copying only the views to the folder deployed in the main Views application (in the corresponding subcategory, directory) and the main project DLL for the main deployable bin applications (dependent DLLs already exist).

Edit:

After running the process on another system, to check it and how it works for .NET 4.5 and VS 2013, I noticed the following:

  • I think I forgot to mention that the link 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 how much Iโ€™ve been convinced of this enough, but links to private (Copy Local) DLL files should be set to โ€œCopy localโ€ and / or should use the version presented in the packages when creating the project from the MVC4 template. I donโ€™t know why, but standard .NET versions (not private) do not seem to work as far as Intellisense is concerned.
  • The best way to check if everything is still OK, as far as the IDE is concerned, is to close the solution, delete the bin and obj folders in the output of the user project, LabelFor solution, cursor on LabelFor in the Index.vbhtml file and press F12, to see if it takes you to the Object Browser.

Due to new versions, the package 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> 
+6
Jan 14 '14 at 19:34
source share

Ok, this is a long shot, but I ran into the same problem. Are you using JetBrains Resharper?

Resharper overrides the VS intellisense engine, but does not understand the Razor syntax. You just need to tell VS to rely on your own intellisense, not Resharper's.

In VS2010, go to Resharper - Options - Intellisense - General. Then check the Visual Studio switch.

Worked great for me.

+4
Jan 14 2018-11-11T00:
source share

None of the solutions I could find on the Internet or on SO has fixed this for me.

It might 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 are working fine.

+3
Nov 07
source share



All Articles