Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

We have a modular architecture where we have some views (cshtml) files in a separate project (class library). How can we get the syntax highlighting and autocomplete to work when the project isn't an MVC project?

Please note that the class library has controllers, views, models etc. It just doesn't have the web.config, global.asax, etc that a normal mvc project would have.

The intellisense works for everything but the so important model: screenshot of model error

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

C:\...\Index.cshtml: ASP.NET runtime error: There is no build provider registered for the extension '.cshtml'. You can register one in the <compilation><buildProviders> section in the machine.config or web.config. Make sure is has a BuildProviderAppliesToAttribute attribute which includes the value 'Web' or 'All'.

So I added this:

<compilation>     <assemblies>       <add assembly="System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />     </assemblies>     <buildProviders>       <add          extension=".cshtml"          type="System.Web.WebPages.Razor.RazorBuildProvider, System.Web.WebPages.Razor"/>     </buildProviders>   </compilation> 

Then after adding the build provider, this error message appears:

C:\...\Index.cshtml: ASP.NET runtime error: Could not load file or assembly 'System.Web.WebPages.Razor' or one of its dependencies. The system cannot find the file specified. (C:\...\machine.config line 259)

like image 951
JarrettV Avatar asked Nov 11 '10 19:11

JarrettV


People also ask

Why is Razor View Engine useful?

It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML. It's just that ASP.NET MVC has implemented a view engine that allows us to use Razor inside of an MVC application to produce HTML.

Which view engine is better Razor or ASPX?

The Razor View Engine is a bit slower than the ASPX View Engine. Razor provides a new view engine with streamlined code for focused templating. Razor's syntax is very compact and improves readability of the markup and code. By default MVC supports ASPX (web forms) and Razor View Engine.

What does the Razor View Engine consists of?

Razor View Engine is a markup syntax which helps us to write HTML and server-side code in web pages using C# or VB.Net. It is server-side markup language however it is not at all a programming language.


1 Answers

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> 
like image 115
Deepak Avatar answered Sep 28 '22 20:09

Deepak