我可以使用Razor提供.html文件,好像它们是.cshtml文件而不更改我所有页面的扩展名吗?

时间:2022-06-01 21:09:30

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

我管理一个大型的asp.net网站,该网站之前已经从静态html网站转换为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.

由于几个原因(主要是SEO),我们决定在最初转换网站时不将所有文件重命名为.aspx。只需将buildProvider和httpHandler添加到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.

现在我正在升级网站以使用带有Razor cshtml文件的Asp.net WebPages。我可以根据需要重命名所有文件,并使用url重写使网址保持不变,但是如果我可以配置web.config告诉它解析.html文件就好像它们是.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.

我已经搜索了很多,并且找不到任何与剃须刀页面的PageHandlerFactory相同的东西。它看起来好像只是.net 4.0 ISAPI处理程序中的内部机制。

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.

该网站目前正在Windows 2003服务器和IIS 6上运行。我们将在不久的将来升级到2008 / IIS 7.5,但我不想等待。

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

有没有办法让.html文件被razor解析,好像它们是.cshtml文件一样?

3 个解决方案

#1


16  

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.

感谢SLaks指出我正确的方向,但是仍然花了几个小时挖掘MVC源来找出解决方案。

1 - Need to put RazorBuildProvider in web.config

1 - 需要将RazorBuildProvider放在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.

并将System.Web.WebPages.Razor添加到程序集(如果它尚不存在)。

<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

2 - 在global.asax Application_Start()方法中添加2行

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

WebPageHttpHandler.RegisterExtension("html");

#2


6  

Call WebPageHttpHandler.RegisterExtension.

调用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.

您可能还需要注册一个自定义WebPageRazorHostFactory来告诉Razor引擎如何处理该文件;我不确定。

#3


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 :(

因为这实际上已经解决了VS2012 / .net 4.5的使用。在C#5项目中使用上面的例子我没有运气:(

#1


16  

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.

感谢SLaks指出我正确的方向,但是仍然花了几个小时挖掘MVC源来找出解决方案。

1 - Need to put RazorBuildProvider in web.config

1 - 需要将RazorBuildProvider放在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.

并将System.Web.WebPages.Razor添加到程序集(如果它尚不存在)。

<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

2 - 在global.asax Application_Start()方法中添加2行

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

WebPageHttpHandler.RegisterExtension("html");

#2


6  

Call WebPageHttpHandler.RegisterExtension.

调用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.

您可能还需要注册一个自定义WebPageRazorHostFactory来告诉Razor引擎如何处理该文件;我不确定。

#3


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 :(

因为这实际上已经解决了VS2012 / .net 4.5的使用。在C#5项目中使用上面的例子我没有运气:(