We have a modular architecture where we have some views (cshtml) files in a separate project (class library). How can we get the syntax highlighting and autocomp
For Visual Studio 2012/ASP.NET MVC 4, you need to update the assembly versions and add <add key="webpages:Version" value="2.0.0.0" />
to appSettings
. Here's what my Web.config looks like:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
</appSettings>
<system.web>
<compilation targetFramework="4.5">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
</system.web>
</configuration>
I have followed Jammer's suggestion and am documenting what I believe is the minimal set of actions to get a project suitable for serving as an ASP.NET MVC4 class library project. This was done in Visual Studio 2012 Update 4, and was targeting VB.Net. I may later include documentation for doing something similar in Visual Studio 2013 if I get a chance. Here are the actions I took:
packages
directory with updated versions of reference files is part of the project).App_Start
folderApp_Data
folder and any other empty folders (my Mercurial history didn't make this visible so I'm going from memory).Global.asax
Global.asax.vb
Web.config
file in the root and the dependent Web.Debug.config
and Web.Release.config
files. (Do not delete Web.config
from the Views folder.)Web.config
file in the Views folder:
appSettings
system.web
system.webServer
<add namespace="System.Web.Mvc.Ajax" />
and <add namespace="System.Web.Routing" />
packages
directory and have Copy Local
and Specific Version
set to True
).
System.Web.Entity
System.Web.ApplicationServices
System.ComponentModel.DataAnnotations
System.Data.DataSetExtensions
System.Web.Extensions
System.Web.Extensions.Design
System.Xml.Linq
System.Web.Abstractions
System.Web.Routing
System.Configuration
System.Web.Services
System.EnterpriseServices
Microsoft.Web.Infrastructure
(1.0.0.0) *Microsoft.Web.Mvc.FixedDisplayModes
(1.0.0) *Newtonsoft.Json
(4.5.11) *System.Net.Http
(2.0.20710.0) *System.Net.Http.Formatting
(4.0.20710.0) *System.Net.Http.WebRequest
(2.0.20710.0) *System.Web.Helpers
(2.0.20710.0) *System.Web.Http
(4.0.20710.0) *System.Web.Http.WebHost
(4.0.20710.0) * 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
packages.config
:
This leaves me with the following:
Copy Local
and Specific Version
set to True
):
Controllers
folder containing CustomUIController.vb
Models
folder containing CustomUIModel.vb
Views
folder containing CustomUI
folder, containing Index.vbhtml
Web.config
file in the Views folder. See below for content.packages.config
file in the root of the project. See below for content.The contents of my files are as follows:
<?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>
<?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>
Imports System.Web.Mvc
Public Class CustomUIController
Inherits Controller
Public Function Index() As ActionResult
Return View()
End Function
End Class
Public Class CustomUIModel
Public Property Name As String
Public Property Value As Decimal
End Class
@ModelType CustomTemplate.CustomUIModel
@Html.LabelFor(Function(m) m.Name)
At this point I am able to work with Intellisense assisting me in the .vbhtml views and the .vb classes, build the project, then copy just the views to the primary application's deployed Views
folder (in the appropriate sub-directory), and the project's primary output DLL to the primary application's deployed bin
directory (the dependent DLL files are already there).
Edit:
After following the process on another system to validate it and how it works for .NET 4.5 and VS 2013, I have noticed the following:
System.Web.DynamicData
can be removed.bin
and obj
folders in the custom project's output, re-load the solution, put the cursor on LabelFor
in the Index.vbhtml file, and press the F12 key to see if it takes you to the Object Browser.Because of the new versions, the packages file is different:
<packages>
<package id="Microsoft.AspNet.Mvc" version="4.0.30506.0" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="2.0.30506.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages" version="2.0.30506.0" targetFramework="net45" />
</packages>
Alright, this is a longshot, but I ran across the same problem. Are you using JetBrains Resharper?
Resharper overrides VS's intellisense engine but it doesn't understand Razor syntax. You simply have to tell VS to rely on its own intellisense rather than Resharper's.
In VS2010, go Resharper - Options - Intellisense - General. Then check the Visual Studio radio button.
Worked great for me.
The webconfig from this post will work. I've copied it below (for posterity):
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
</system.web.webPages.razor>
<system.web>
<compilation targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</assemblies>
</compilation>
</system.web>
</configuration>
None of the solutions I could find online or on SO fixed this for me.
May seem like a sledgehammer to crack a nut but I created an MVC 4 application project instead of a class library and ripped out everything I didn't need. Intellisense and @model
working fine.