Rewrite all urls change aspx extension to html

2019-06-01 08:04发布

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.

3条回答
闹够了就滚
2楼-- · 2019-06-01 08:29

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
查看更多
Emotional °昔
3楼-- · 2019-06-01 08:30

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");
    }
}
查看更多
家丑人穷心不美
4楼-- · 2019-06-01 08:50

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.

查看更多
登录 后发表回答