我只是不明白爲什麼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]);
}
}
}
更新: 我仍然試圖解決這個問題,我試圖在不同操作系統的其他機器上運行此代碼仍然沒有運氣
你有沒有進入代碼並調試它?它是否正確地擊中了你的重寫代碼?它是通過它嗎? –
是的,我做到了。其實我在這裏看到的東西,我不知道如何處理它: 當我檢查app.context我看到了這個:App.Context.IsWebSocketRequest'App.Context.IsWebSocketRequest'拋出了一個異常'System.InvalidOperationException ' 你能幫我解決這個錯誤嗎? –