我可以用剃刀成为HTML文件,好像他们是.cshtml文件,而无需改变我的所有页面的扩展? [英] Can I serve .html files using Razor as if they were .cshtml files without changing the extension of all my pages?

查看:498
本文介绍了我可以用剃刀成为HTML文件,好像他们是.cshtml文件,而无需改变我的所有页面的扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我管理有previously从静态的HTML网站转换成asp.net大asp.net网站。

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

由于多种原因(主要是搜索引擎优化),我们决定不重命名所有的文件为.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>

现在我升级到使用Asp.net网页剃刀CSHTML文件的网站。如果需要的话我可以重命名的所有文件,并使用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 Server和IIS 6上正在运行的,我们将升级到2008 / IIS在不久的将来7.5,但我preFER不要等待。

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文件被剃刀解析?

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 - 添加2行的Global.asax 的Application_Start()

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

WebPageHttpHandler.RegisterExtension("html");

这篇关于我可以用剃刀成为HTML文件,好像他们是.cshtml文件,而无需改变我的所有页面的扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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