ASP.NET WebPages use html extension

允我心安 提交于 2019-12-01 06:49:08

问题


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.


回答1:


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>



回答2:


You want to use routing. Are you using webforms or MVC? Global.asax is a good start. Add the complete code here:

namespace Name
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes(RouteTable.Routes);
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void RegisterRoutes(RouteCollection routes)
        {
            routes.MapPageRoute("Route1", "OldPage.html", "~/NewPage.aspx");

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}

Obviously you don't want to manually add 500 routes but you can add url filters.

See: http://msdn.microsoft.com/en-us/library/cc668201.ASPX

ASP.NET routing enables you to use URLs that do not have to map to specific files in a Web site.



来源:https://stackoverflow.com/questions/18626741/asp-net-webpages-use-html-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!