2011-01-23 80 views
0

我正在尋找動態404頁面的解決方案。ASP.NET IIS7動態404頁面

我有一個Page404.aspx頁面,它在加載時請求查詢字符串中的WebsiteID參數。

假設每個網站都有一個不同的404頁面html存儲在數據庫中,並且在每個頁面加載時,它將通過QueryString中的WebsiteID顯示正確的html。

現在很棘手的事情 - 我如何使用CURRENT WebsiteID將客戶端重定向到正確的404頁面?

希望我清楚了。在此先感謝,

Gal。

回答

0

如果您沒有Global.asax,請爲您的網站創建一個。 這假設您的網站按目錄拆分。

在新的Global.asax文件應該有

protected void Application_Error(Object sender, EventArgs e) 
{ 
    HttpContext ctx = HttpContext.Current; 

    Exception exception = ctx.Server.GetLastError(); 

if (ex.GetType() == typeof(HttpException)) 
{ 
    HttpException httpEx = (HttpException)ex; 
    if(httpEx.GetHttpCode() == 404) 
    { 

    int websiteID = FindWebsiteID(ctx.Request.Url.ToString()); 

    string errorPage = string.Format("~/404Page.aspx?={0}",websiteID); 

    Response.Redirect(errorPage); 
    } 
} 

public int FindWebsiteID(string url){ 

//parse the url for the directory and look up the id 
//for the website via the directory name 

} 

您還可以檢查出this article瞭解更多信息。

+0

但是,這將發送每個錯誤404'id = 2` - 如果我的網站是id 46呢?通過在配置文件中對它進行硬編碼,你不會讓它在每個網站上都是動態的。 – 2011-01-23 12:36:47