簡體   English   中英

如何使用ASP.NET 4.0 URL重寫來重寫URL?

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

我在ASP.NET 4.0中構建了一個應用程序。 我需要使用Globax.asax文件中的URL重寫類或IIS 7或IIS 7.5中的Microsoft URL重寫擴展名來重寫url

例。

我有一個動態生成的url,不幸的是我無法更改,因為它是第三方控件。

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

我需要將其重寫為

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

這是我需要重寫某些內容以偽裝子域的想法時發現的一個示例 以下代碼通常位於您的web.config文件中,也可以通過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" />

基本上發生的是,當請求的url通過IIS時,它將在路徑上進行模式匹配,並將其與該站點具有的所有重寫規則進行比較。 如果該URL與某個模式匹配,則IIS將重寫該URL以使其與規則匹配,並使用新的URL繼續進行請求。

我成功地在幾個站點上使用了它,這對我來說效果很好。

此示例具有一系列屏幕截圖,顯示了您在瀏覽和設置url重寫規則時IIS對話框的外觀。

希望這對您的項目有所幫助。

嘗試這個:

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,您喜歡使用IIS_ISAPI嗎? 如果是,您可以嘗試使用Ionics Isapi重寫過濾器

如果否,則可以使用此URL重寫模塊。 Intelligencia.UrlRewriter

兩者都不是,如果要編寫自己的代碼,則需要實現httpModule interfaceHttpContent.RewritePath方法。

例如:

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);
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM