简体   繁体   English

设置httpmodule以防止某些页面出现404

[英]set up httpmodule to prevent 404's for certain pages

I am currently working on a .net c# web application. 我目前正在开发.net c#Web应用程序。 I have removed several .aspx from the site (deleted them) eg page1.aspx, page2.aspx etc. 我已经从网站上删除了几个.aspx(删除了它们),例如page1.aspx,page2.aspx等。

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. 我现在想做的是创建一个http模块,当用户浏览器调用page1.aspx或page2.aspx时,而不是返回404状态代码,而是为已删除的页面返回适当的状态代码,并将用户重定向到主页,而不是错误页面。

What is the best way of achieving this, is it via a http module and if so, I would I set such up? 实现此目的的最佳方法是什么,是否通过http模块实现,如果可以,我将进行设置吗?

If there are only a few pages you could just leave them there with Response.StatusCode = 410 and a simple window.location = basepage . 如果只有几页,则可以使用Response.StatusCode = 410和一个简单的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. 只需将其放入您的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. 然后,如果需要,添加更多错误也很容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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