【问题标题】:Redirect to 404 Page for a specific resources重定向到特定资源的 404 页面
【发布时间】:2013-10-23 11:35:07
【问题描述】:

我如何为特定请求强制 404 错误?

我不希望从网站中排除该页面。

但是,如果用户请求以下两个文件,我总是会返回 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 页面的最佳方法是什么?

【问题讨论】:

  • 您想将文件留在那里但不想处理它们?为什么?为什么不能简单地重命名它们 - 那么 404 就会自然而然地出现?
  • @Olaf,我稍后会使用相同的文件。我不想重命名,也不想在 web.config 中配置它。请添加一些建议以处理 web.config 以外的 404。
  • 如果它们不应该存在,你能从后面的代码中重定向它们吗?你在使用角色管理吗?如果您想要 404 来获取实际存在的资源,我看不出 web.config 将如何帮助您。
  • 我会按照@MikeSmithDev 的建议从后面的代码中做到这一点。也可以通过实现自定义 http 模块来实现。
  • @Billa 你当然知道,with a HttpHandler。您还可以使用其所在文件夹的 web.config 条目锁定 PDF。

标签: c# asp.net url-rewriting


【解决方案1】:

为方便起见,请将其放入您的 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>

按照 MikeSmithDev 的建议,更强大的解决方案是使用 HttpHandler,这样您就可以控制用户是否是 404 用户。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多