ASP.NET IIS7动态404页面

时间:2011-01-23 11:59:43

标签: asp.net iis-7 http-status-code-404

我正在寻找动态404页面的解决方案。

我有一个Page404.aspx页面,在加载时会在查询字符串中请求一个WebsiteID参数。

假设每个网站都有一个不同的404页面html存储在数据库中,并且在每个页面加载时,它将通过QueryString中的WebsiteID显示正确的html。

现在是棘手的事情 - 如何使用CURRENT WebsiteID将客户端重定向到正确的404页面?

希望我足够清楚。提前谢谢,

加。

1 个答案:

答案 0 :(得分: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以获取更多信息。

相关问题