简体   繁体   中英

set up httpmodule to prevent 404's for certain pages

I am currently working on a .net c# web application. I have removed several .aspx from the site (deleted them) eg page1.aspx, page2.aspx etc.

What I want to do now is create a http module that when the page1.aspx or page2.aspx is called by a users browser, instead of returning a 404 status code, return the appropriate status code for a deleted page and redirect the user to the home page, rather than an error page.

What is the best way of achieving this, is it via a http module and if so, I would I set such up?

If there are only a few pages you could just leave them there with Response.StatusCode = 410 and a simple window.location = basepage .

If it is a dynamic list that can grow large i would put it in your application error handler:

protected void Application_Error(Object sender, EventArgs e)
{
    if (/*requesting a deleted resource*/) 
    {
        //return status 410 and a javascript redirect
    }
}

I think this is the best way. and it's actually very simple. just put this in your webconfig.

<customErrors mode="On" defaultRedirect="~/TheDefaultErrorPage.aspx"> 
    <error statusCode="404" redirect="The404ErrorPage.aspx"/> 
</customErrors>

Then it's also easy to add more errors if you want.

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