bevacqua
bevacqua

Reputation: 48496

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.

Upvotes: 8

Views: 2671

Answers (2)

Mahmood Dehghan
Mahmood Dehghan

Reputation: 8265

This is a nice guide to achieve this.

http://thetoeb.de/2014/01/05/enabling-mvc5-intellisense-in-a-classlibrary-project/

The steps are:

  1. 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”)
  2. Close any and all open .cshtml files
  3. Right click project -> Properties -> Build -> change Output path to “bin/”
  4. 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)
  5. 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>

Upvotes: 3

Nenad
Nenad

Reputation: 26657

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)' != ''" />

Upvotes: 3

Related Questions