0

特定のリクエストに対して 404 エラーを強制するにはどうすればよいですか?

このページをサイトから除外したくありません。

ただし、ユーザーが以下の 2 つのファイルを要求した場合は、常に 404 エラー ページが返されます。NotFound.aspx

/site/employee/pensionplan.aspx

/site/employee/pensiondoc.pdf

と言われましたnot to use the web.config for 404 error configuration, due to some security issues。正確に何が問題なのかわかりません

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

Not Foundユーザーが特定のリソースにアクセスしようとしたときに、ユーザーをページにリダイレクトする最善の方法は何ですか?

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>

より堅牢なソリューションはHttpHandler、MikeSmithDev が推奨するように を使用することです。これにより、ユーザーが 404 かどうかを制御できます。

于 2013-10-15T17:13:13.263 に答える