简体   繁体   中英

How to rewrite URL using of ASP.NET 4.0 URL rewriting?

I have an application build in ASP.NET 4.0. And I need to rewrite url using URL rewriting class in Globax.asax file or with microsoft url rewriting extension in IIS 7 or IIS 7.5

Example.

I have a dynamically build url, which unfortunately I cannot change, because it's third party control.

http://sitename.com/store/description/product?table=page2

I need to rewrite it to

http://sitename.com/store/description/product?id=2

This is an example that I found when I needed to do some rewriting to fake out the idea of a subdomain. The following code usually goes in your web.config file and can also be setup through IIS7 management studio.

<system.webServer>
 <validation validateIntegratedModeConfiguration="false"/>
 <modules runAllManagedModulesForAllRequests="true"/>
  <rewrite>
   <rules>
     <clear />
    <!-- Ameritexintl Website Publisher -->
  <rule name="ameritexintl-Web-publisher" enabled="true" stopProcessing="true">
       <match url="(.*)" />
       <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
         <add input="{HTTP_HOST}" pattern="^www\.publisher\.ameritexintl\.com$" />
       </conditions>
       <action type="Redirect" url="http://publisher.ameritexintl.com/{R:0}" />
     </rule>

    <rule name="ameritexintl-Web-publisher-rewrite" enabled="true" stopProcessing="true">
       <match url="(.*)" />
       <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
         <add input="{HTTP_HOST}" pattern="^(www\.)?publisher\.ameritexintl\.com$" />
         <add input="{PATH_INFO}" pattern="^/publisher/($|/)" negate="true" />
       </conditions>
       <action type="Rewrite" url="/publisher/{R:0}" />
     </rule>
   </rules>
 </rewrite>
 <urlCompression doStaticCompression="true" doDynamicCompression="true" />

Basically what is happening is that when a requested url goes through IIS, it will do a pattern match on the path and compare it to any rewrite rules it has for the site. If the url matches a pattern, IIS will then rewrite the url to match to the rule and push the request on through with the new url.

I am sucessfully using this on a couple of sites and this works quite well for me.

This example has a series of screen captures for what the IIS dialog will look like when you're going through and setting up your url rewrite rules.

Hope this helps some, and good luck on your project.

Try this:

string data = @"http://sitename.com/store/description/product?table=page2";
string pattern = @"(table)(?:=)([^\d]+)";

Console.WriteLine ( Regex.Replace(data, pattern, "id="));

// Result
// http://sitename.com/store/description/product?id=2

@Eugene,do you like use the IIS_ISAPI? if yes,you can try this Ionics Isapi Rewrite Filter

if no,you can use this url rewrite module. Intelligencia.UrlRewriter

neither,If you want to write their own code,you need implementation the httpModule interface and HttpContent.RewritePath method.

for example:

public sealed class RewriterHttpModule : IHttpModule
    {
    private static RewriterEngine _rewriter = new RewriterEngine();

    public void Dispose()
    {
    }

    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(BeginRequest);
    }

    private void BeginRequest(object sender, EventArgs e)
    {
       var context=((HttpApplication)sender).Context;
       string path = context.Request.Path;
       /*
         url rewrite list: 
          Dictionary<string,string>
       */
        Dictionary<string, string> urls = new Dictionary<string, string>();
        urls.Add(@"/store/description/product?table=page(\d+)", "/store/description/product?id=$1");
        foreach (var pair in urls)
        {
            if (Regex.IsMatch(path, pair.Key))
            {
                var newUrl = Regex.Replace(path, pair.Key, pair.Value);
                //rewrite url
                context.RewritePath(newUrl, false);
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM