is there a certain way to configure web.config and make Razor available in ASP.Net 4 normal Web Application?

StackOverflow https://stackoverflow.com/questions/10427771

Question

NOT Web Pages Application - I used VisualStudio 2010 and it is a multi-projects solution.

Now the Web Application is in .Net 4.0 but with over 200+ aspx files in it, today I was just trying to add a few Razor references and see whether it could work - surprisingly it gave me a feeling that it might be possible?

here's the web.config file I made, however it gave me "This type of page is not served." error. can anyone give me a hand to see if it is possible?

here's my modified 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=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>
  <connectionStrings>
  </connectionStrings>
  <appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>
  <system.web.webPages.razor>
    <host factoryType="System.Web.WebPages.Razor.WebRazorHostFactory, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.WebPages.WebPage">
      <namespaces>
        <add namespace="System.Web.WebPages" />
        <add namespace="System.Web.WebPages.Html" />
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>
  <system.web>
    <httpRuntime requestValidationMode="2.0" maxRequestLength="1048576000" executionTimeout="99999" />
    <customErrors mode="Off" />
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>
    <pages validateRequest="false">
      <namespaces>
        <add namespace="System.Web.WebPages" />
        <add namespace="System.Web.WebPages.Html" />
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
    <httpHandlers>
      <add path="*.cshtml" verb="GET,HEAD,POST,DEBUG" type="System.Web.HttpForbiddenHandler" />
    </httpHandlers>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true">
      <add name="PBHttpModule" type="WebCider.PortalBuilder.Framework.Web.Http.PBHttpModule, WebCider.PortalBuilder.Framework.Web" />
    </modules>
    <directoryBrowse enabled="true" />
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="1048576000" />
        <fileExtensions>
          <add fileExtension=".cshtml" allowed="true" />
        </fileExtensions>
      </requestFiltering>
    </security>
    <handlers>
    </handlers>
  </system.webServer>
  <runtime>
  </runtime>
</configuration>
Was it helpful?

Solution

You don't really need to modify anything in your current web forms application. You could simply start by adding a ~/Foo.cshtml Razor file to the root with the following contents for example:

@DateTime.Now

and then simply navigate to http://example.com/Foo.cshtml. The <system.web.webPages.razor> sections that you have added to your web.config are useless in a WebForms application because you don't have controllers that return view results that render a System.Web.WebPages.WebPage instance. Obviously due to the this fact you cannot use HTML helpers inside those Razor templates. It's just simple Razor as you get in WebMatrix.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top