0

How i do force 404 error for a particular request?

I don't wish to exclude the page from the site.

However if the user requested for the below two files, I would always return 404 Error Page NotFound.aspx

/site/employee/pensionplan.aspx

/site/employee/pensiondoc.pdf

I have been told that not to use the web.config for 404 error configuration, due to some security issues. I am not sure what exactly the problem

<!-- Turn on Custom Errors -->
<customErrors mode="On" 
  defaultRedirect="DefaultRedirectErrorPage.aspx">
  <error statusCode="404" redirect="Http404ErrorPage.aspx"/>
</customErrors>

What is the best way to redirect a user to a Not Found page when he trying to access a particular resource?

4

1 回答 1

0

为方便起见,请将其放入您的Http404ErrorPage.aspx代码隐藏中:

protected void Page_Load(object sender, EventArgs e)
{
    Response.StatusCode = 404;
}

并在你的 web.config 中有这个:

<location path="site/employee/pensionplan.aspx">
<system.webServer>
  <httpRedirect enabled="true" destination="http://www.website.com/Http404ErrorPage.aspx" httpResponseStatus="Permanent" />
</system.webServer>
</location>
<location path="site/employee/pensiondoc.pdf">
<system.webServer>
  <httpRedirect enabled="true" destination="http://www.website.com/Http404ErrorPage.aspx" httpResponseStatus="Permanent" />
</system.webServer>
</location>

一个更强大的解决方案是使用HttpHandlerMikeSmithDev 推荐的,这样您就可以控制用户是否是 404 的。

于 2013-10-15T17:13:13.263 回答