Rewrite all urls change aspx extension to html

2019-06-01 07:59发布

问题:

I'm new in the iis url rewrite module and i not how to do this.

for example i have this url:

http://localhost/section.aspx?x=section1&IDSection=45

And i want this:

http://localhost/section~x~section11~IDSection~45.html

Any ideas? Thanks for your help.

回答1:

What you need to do is write a handler. That way you can capture the extension and then parse it as needed. This will be invisible to the user. Handler is definitely the way you want to go as if you use URL Routing, you will still need to change the handler from aspx to html in IIS.



回答2:

this is the solution using URL Rewrite module in IIS7:

  • Create new blank inbound rule
  • The patterns is: ^section~x~([_0-9a-z-]+)~IDSection~([0-9]+).html
  • The rewrite action: Section.aspx?x={R:1}&IDSection={R:2}
  • Create new black outbund rule
  • Create new precondition with the next format:
    • Condition input: {RESPONSE_CONTENT_TYPE}
    • Pattern: ^text/html
  • The pattern is: ^Section.aspx\?x=([_0-9a-z-]+)(?:&|&)IDSection=([0-9]+)$
  • And the rewrite action: Section~x~{R:2}~IDSection~{R:2}.html


回答3:

You can do this in c# to use a customized extension in your URL in ASP.NET.

protected void Application_BeginRequest(object sender, EventArgs e)
   {
    HttpApplication app = sender as HttpApplication;
    if (app.Request.Path.ToLower().IndexOf(".html") > 0)
    {
        string rawpath = app.Request.Path;
        string path = rawpath.Substring(0, rawpath.IndexOf(".html"));
        app.Context.RewritePath(path+".aspx");
    }
}