再次試驗用ASP.Net來Redirect
以前我曾經寫過文章介紹如何使用Global.asax來做重定向。可是實踐中出現很多問題。
新的項目我使用HttpModule來做。使用HttpModule而不是HttpHandler可以避免無限循環的問題。HttpHandler其實是截獲Handler,所有後來的事都要自己手工解決。我只是重新定向,沒有這麼復雜。HttpModule看上去比較實惠,所以就是這個了。

public class HttpModule : IHttpModule


...{

public void Dispose()


...{ }


public void Init(HttpApplication context)


...{

context.AcquireRequestState += new EventHandler(context_BeginRequest);

}


private void context_BeginRequest(object sender, EventArgs e)


...{

string From = Config.GetAPPSettings("From");

string To = Config.GetAPPSettings("To");

string WebAppPath = HttpContext.Current.Request.ApplicationPath;

From = From.Replace("~/", "");

To = To.Replace("~/", WebAppPath + "/");


if (HttpContext.Current.Request.RawUrl.Contains(From))


...{

HttpContext.Current.Server.Transfer(To, true);

}

}

}
只要在Web.config裡面加上From和To的APPSetting就可以了。Web.config中加入如下httpModule:
<add name="HttpModule" type="HttpModule, MyLib" />
前面是類,後面是dll的文件名。如果IIS中沒有這個虛擬文件,要這樣設置:打開ISAPI,設置Mapping,勾選Check that file exists, 這樣,打開浏覽器就可以訪問到假地址。