简体   繁体   中英

ASP.NET IIS7 Dynamic 404 Page

I'm looking for a solution for a dynamic 404 page.

I have a Page404.aspx page that requests a WebsiteID parameter in the querystring when it loads.

Let's say each Website has a different 404 page html stored in DB, and in each page load it will show the correct html by the WebsiteID in the QueryString.

Now the tricky thing- How do I redirect the client to the correct 404page, with the CURRENT WebsiteID ?

Hope I was clear enough. Thanks in advance,

Gal.

If you don't have a Global.asax, then create one for your website. This assumes that your websites are split up by directory.

In the new Global.asax file there should be

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

}

you may also check out this article for additional information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM