ASP.NET WebPages使用html扩展名 [英] ASP.NET WebPages use html extension

查看:71
本文介绍了ASP.NET WebPages使用html扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ASP.NET网页来理解现有站点它使用静态.html文件(其中约有500个).不幸的是,我的SEO人员要求该网站维护其现有目录/文件名,因此我需要使用.html.

找到此示例后,我尝试在 web.config 中的 compilation/buildProviders/为:

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

还要添加一个程序集:

 < add assembly ="System.Web.WebPages.Razor,版本= 1.0.0.0,文化=中性,PublicKeyToken = 31BF3856AD364E35"/> 

这仍然不会呈现页面.它只是作为源输出.我还在项目的根目录下创建了一个 Global.asax 并将其添加到 Application_Start():

  System.Web.Razor.RazorCodeLanguage.Languages.Add("html",新的System.Web.Razor.CSharpRazorCodeLanguage());System.Web.WebPages.WebPageHttpHandler.RegisterExtension("html"); 

仍然无效.不幸的是,我在Google上空白.

解决方案

我在尝试解决同一问题时碰到了这个问题-尽管出于我的好奇心.

这是您在web.config文件中所需要的:

 < system.web><编译>< buildProviders>< add extension =.html"type ="System.Web.WebPages.Razor.RazorBuildProvider,System.Web.WebPages.Razor"/></buildProviders></compilation></system.web>< system.webServer><处理程序><添加name ="HTML"动词="*" path ="*.html"type ="System.Web.Webpages,WebPageHttpHandler"/></handlers></system.webServer> 

但是,仅凭这还不够!我们需要向 WebPageHttpHandler 注册扩展名.
通常,您可以在 _AppStart 文件中执行类似的操作-不幸的是,当应用程序启动时(即,当 _AppStart 执行时),它将遍历其中的项目WebPageHttpHandler的SupportedExtensions,因此我们实际上无法在AppStart中注册该扩展.
我所做的是使用 PreApplicationStartMethod 属性制作了一个新的.dll程序集,如 RazorCodeLanguage.Languages 词典中,以便Razor引擎可以弄清楚如何编译模板.

Global.asax文件示例:

 <%@应用程序语言="C#"%>< script runat ="server">无效的Application_Start(对象发送者,EventArgs e){System.Web.WebPages.WebPageHttpHandler.RegisterExtension("html");var语言= System.Web.Razor.RazorCodeLanguage.Languages;语言.Add("html",语言["cshtml"]);}</script> 

I'm trying to use ASP.NET WebPages to make sense of an existing site which uses static .html files (about 500 of them). Unfortunately, my SEO person is requiring that the site maintains its existing directory / filenames, so I need to use .html.

After finding this example, I tried adding the extension in web.config under compilation/buildProviders/ as:

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

And adding an Assembly as well:

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

This still doesn't render the page. It is simply output as source. I also created a Global.asax at the root of the project and added this under Application_Start():

System.Web.Razor.RazorCodeLanguage.Languages.Add(
    "html", new System.Web.Razor.CSharpRazorCodeLanguage());
System.Web.WebPages.WebPageHttpHandler.RegisterExtension("html");

Still had no effect. Unfortunately, I'm drawing a blank on Google.

解决方案

I happened upon this question while trying to solve the same problem - although in my case, for curiosity's sake.

Here's what you need in your web.config file:

<system.web>
   <compilation>
      <buildProviders>
         <add extension=".html"
              type="System.Web.WebPages.Razor.RazorBuildProvider, System.Web.WebPages.Razor"/>
      </buildProviders>
   </compilation>
</system.web>
<system.webServer>
   <handlers>
      <add name="Html" verb="*" path="*.html"
           type="System.Web.Webpages, WebPageHttpHandler"/>
   </handlers>
</system.webServer>

This isn't enough on its own, though! We need to register the extension with WebPageHttpHandler.
Normally, you'd be able to do stuff like this in the _AppStart file - unfortunately, when the application starts (i.e when _AppStart executes), it iterates over the items in the SupportedExtensions of WebPageHttpHandler, so we can't actually register the extension in AppStart.
What I did is I made a new .dll assembly with the PreApplicationStartMethod attribute, as seen here, but you can also do it inside the Global.asax file's Application_Start method.

Finally, we also need to add "html" as an extension to the RazorCodeLanguage.Languages dictionary, so that the Razor engine can figure out how to compile the template.

Example Global.asax file:

<%@ Application Language="C#" %>
<script runat="server">
   void Application_Start(object sender, EventArgs e) 
   {
      System.Web.WebPages.WebPageHttpHandler.RegisterExtension("html");
      var languages = System.Web.Razor.RazorCodeLanguage.Languages;
      languages.Add("html", languages["cshtml"]);
   }       
</script>

这篇关于ASP.NET WebPages使用html扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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