2013-10-15 59 views
0

我如何強制執行特定請求的404錯誤?重定向到特定資源的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頁面時,他試圖訪問特定資源重定向的最佳方法?

+0

你想離開那裏的文件,但不想處理它們?爲什麼?爲什麼你不能簡單地重命名它們 - 那麼404會自然而然地出現? – Olaf

+0

@Olaf,我稍後使用相同的文件。我不想重命名,我不想在web.config中配置它。請添加一些建議來處理除web.config以外的404。 – Billa

+1

如果他們不應該在那裏,你可以將他們從後面的代碼重定向嗎?你在使用角色管理嗎?如果你想讓404獲得實際存在的資源,我看不出web.config如何幫助你。 – MikeSmithDev

回答

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'd。