11

I manage a large asp.net site which has previously been converted from static html site to asp.net.

For several reasons (mainly SEO) we decided not to rename all the files to .aspx back when we originally converted the site. This was very easy to do by simply adding the buildProvider and httpHandler to the web.config.

<buildProviders>
  <add extension=".html" type="System.Web.Compilation.PageBuildProvider"/>
</buildProviders>

<httpHandlers>
  <add path="*.html" verb="*" type="System.Web.UI.PageHandlerFactory"/>
</httpHandlers>

Now I am upgrading the site to use Asp.net WebPages with Razor cshtml files. I can rename all the files if necessary, and use url rewriting to make the urls stay the same, however it would be much easier if I could just configure the web.config to tell it to parse .html files as if they were .cshtml.

I have searched around quite a bit, and could not find anything equivalent to the PageHandlerFactory for razor pages. It appears as though it is just an internal mechanism in the .net 4.0 ISAPI handler.

The site is currently running on Windows 2003 server and IIS 6. We will be upgrading to 2008/IIS 7.5 in the near future, but I'd prefer not to wait for that.

Is there any way to get the .html files to be parsed by razor as if they were .cshtml files?

Nick Haslam
  • 1,512
  • 1
  • 24
  • 47
Kevin Lewis
  • 475
  • 4
  • 10

3 Answers3

17

Thank you to SLaks for pointing me in the right direction, but it still took a few hours of digging in the MVC source to figure out the solution.

1 - Need to put RazorBuildProvider in web.config

<buildProviders>
    <add extension=".html" type="System.Web.WebPages.Razor.RazorBuildProvider"/>
</buildProviders>

And add System.Web.WebPages.Razor to assemblies if it isn't already there.

<assemblies>
     [...]
     <add assembly="System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>

2 - Add 2 lines in global.asax Application_Start() method

// Requires reference to System.Web.WebPages.Razor
System.Web.Razor.RazorCodeLanguage.Languages.Add(
    "html", new CSharpRazorCodeLanguage());

WebPageHttpHandler.RegisterExtension("html");
Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442
Kevin Lewis
  • 475
  • 4
  • 10
  • I'm returning a css file instead of cshtml file. Is this approach valid for MVC3? If yes, which web.config the one in the root folder or under Views folder? And where exactly in the web.config. I tried to do this but it did not add the extension to the languages list. Any ideas? – Idrees Dec 12 '11 at 12:28
  • @Idrees: Yes. The root Web.config. Which languages list? – SLaks Dec 12 '11 at 17:09
  • @SLaks the team leader dropped the idea. So COOL :D – Idrees Dec 13 '11 at 09:17
  • And then you need to overwrite the default razor view engine and add .html to ViewLocationFormats and PartialViewLocationFormats – Stefan Steiger Apr 21 '13 at 16:18
  • Thank you! This got rid of the damn blue squigglies while editing a cshtml page, that's in a separate project from the main project. – John T May 30 '13 at 14:10
  • I get the error "The type or namespace name 'Razor' does not exist in the namespace 'System.Web'" regarding the updates to global.asax (.NET 4.0) – Scribblemacher Jan 07 '16 at 16:16
  • Figured it out. Apparently you need to have at least one page with the .cshtml exception. Creating an empty file named "dummy.cshtml" solved this (but that probably wasn't what I was supposed to do...) – Scribblemacher Jan 07 '16 at 16:38
  • 1
    @KevinLewis where in the `web.config` file should I put the `` config? I am trying to do the same thing, generating `md` files from `csmd` using razor syntax, I want to have the IntelliSense available. Is this the way to go? – Luiso Jun 18 '16 at 20:01
6

Call WebPageHttpHandler.RegisterExtension.

You may also need to register a custom WebPageRazorHostFactory to tell the Razor engine what to do with the file; I'm not sure.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

As this actually been resolved for use with VS2012 / .net 4.5. As using the examples above in a C#5 project I get no luck :(