Search code examples
asp.netasp.net-webpagesrazor-2

Razor 2 with Web Pages 2


I cannot get Razor 2 view engine to work with my web pages project. I installed Web Matrix and everything but it's obvious that it's the Razor 1 engine that is used beacuse the tilde feature (href="~/style.css") is not working.

I've tried to modify my config file and to reference the Razor 2 dll files but it's still not working.

This is what my config file 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>

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

  <appSettings>
    <add key="webpages:Enabled" value="true"/>
  </appSettings>

  <system.web>
    <compilation debug="true"/>
  </system.web>

</configuration>

Solution

  • When you look at System.Web.WebPages.dll in your bin folder, which version is there? (Mine is v2.0.20710.0 for instance)

    Not sure why you would have an older version there if you are working with an up-to-date Webmatrix v2 Rel 2, but you can find the assemblies you are interested in here:

    C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v2.0\Assemblies
    


    update based upon comment-thread:

    What Adrian Rosca eventually did to make this work in VS2012 was to reference all following assemblies and make them "Copy Local":

  • System.Web.Razor.dll
  • System.Web.WebPages.Deployment.dll
  • System.Web.WebPages.dll
  • System.Web.WebPages.Razor.dll.


    edit:

    You might want to try adding the targetFramework="4.0" to your web.config compilation entry:

     <compilation debug="true" targetFramework="4.0" />
    

    Check out the following SO post to read more on it and, depending on where you deploy your web application, whether or not it really matters to you:
    What happens if I don't specify targetFramework="4.0"?