2016-08-11 155 views
2

我只是不明白爲什麼RewritePath方法在這段代碼中不起作用。 當我嘗試從瀏覽網頁ProductPage.aspx項目,在地址欄中的網址仍顯示爲http://localhost:44789/ProductPage.aspx的而不是這樣的: http://localhost:44789/ProductPage.aspx/?color=URL重寫不起作用

這裏是我的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

/// <summary> 
/// Summary description for GetProductInfo 
/// </summary> 
public class GetProductInfo:IHttpModule 
{ 
    public GetProductInfo() 
    { 
     // 
     // TODO: Add constructor logic here 
     // 
    } 

    public void Dispose() 
    { 
     throw new NotImplementedException(); 
    } 

    public void Init(HttpApplication context) 
    { 
     context.BeginRequest += Context_BeginRequest; 

    } 

    private void Context_BeginRequest(object sender, EventArgs e) 
    { 
     HttpApplication App = sender as HttpApplication; 

     if (App.Request.Path.Contains("ProductPage.aspx")) 
     { 

      string[] Parts = App.Request.Path.Split('/'); 
      App.Response.Write(Parts.Length); 
      if (Parts.Length < 3) 
       App.Context.RewritePath("ProductPage.aspx/?Color="); 
      else 
       App.Context.RewritePath("ProductPage.aspx?color=" + Parts[Parts.Length - 1]); 

     } 
    } 
} 

更新: 我仍然試圖解決這個問題,我試圖在不同操作系統的其他機器上運行此代碼仍然沒有運氣

+1

你有沒有進入代碼並調試它?它是否正確地擊中了你的重寫代碼?它是通過它嗎? –

+0

是的,我做到了。其實我在這裏看到的東西,我不知道如何處理它: 當我檢查app.context我看到了這個:App.Context.IsWebSocketRequest'App.Context.IsWebSocketRequest'拋出了一個異常'System.InvalidOperationException ' 你能幫我解決這個錯誤嗎? –

回答

0

簡短的回答:它不會更改瀏覽器的地址欄中的URL。這是一個內部重定向。

長答案閱讀http://www.dotnetperls.com/rewritepathhttps://msdn.microsoft.com/en-us/library/system.web.httpcontext.rewritepath(v=vs.110).aspx

您應該查看IIS URL重寫模塊http://www.iis.net/downloads/microsoft/url-rewrite。這可以做你在你的問題中描述的。

+0

非常感謝。那就是問題所在,誤解了這種方法的真正功能。 –