2011-01-23 79 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