ASP.NET WebPages use html extension

送分小仙女□ 提交于 2019-12-01 08:06:34

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>

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.

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