我可以使用 Razor 提供 .html 文件,就好像它们是 .cshtml 文件一样,而不更改我所有页面的扩展名吗? [英] Can I serve .html files using Razor as if they were .cshtml files without changing the extension of all my pages?

查看:22
本文介绍了我可以使用 Razor 提供 .html 文件,就好像它们是 .cshtml 文件一样,而不更改我所有页面的扩展名吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我管理一个大型 asp.net 站点,该站点以前已从静态 html 站点转换为 asp.net.

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

出于多种原因(主要是 SEO),我们决定在最初转换站点时不将所有文件重命名为 .aspx.这很容易做到,只需将 buildProvider 和 httpHandler 添加到 web.config 中即可.

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>

现在我正在升级站点以使用带有 Razor cshtml 文件的 Asp.net 网页.如有必要,我可以重命名所有文件,并使用 url 重写使 url 保持不变,但是如果我可以配置 web.config 让它像解析 .cshtml 一样解析 .html 文件会容易得多.

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.

我已经搜索了很多,但找不到任何与剃刀页面的 PageHandlerFactory 等效的东西.它似乎只是 .net 4.0 ISAPI 处理程序中的一种内部机制.

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.

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

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.

有没有办法让 .html 文件像 .cshtml 文件一样被 razor 解析?

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

推荐答案

感谢 SLaks 为我指明了正确的方向,但仍然需要在 MVC 源代码中挖掘几个小时才能找到解决方案.

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 - 需要将 RazorBuildProvider 放在 web.config 中

1 - Need to put RazorBuildProvider in web.config

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

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

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 - 在 global.asax Application_Start() 方法中添加 2 行

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");

这篇关于我可以使用 Razor 提供 .html 文件,就好像它们是 .cshtml 文件一样,而不更改我所有页面的扩展名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆