2011-12-27 194 views
0

我正在使用當前的網絡應用程序,我們想要確定所請求的頁面是否正確,我們在Application_BeginRequest方法的Global.asax上執行此操作,我們檢查這樣的網址就好像有人輸入http://mywebtest/badurl然後我們將它們發送到一個自定義的404頁面,但是當它擴展名爲.aspx時,我們遇到了麻煩,有些頁面對於aspx擴展是很好的,但是對於不存在的頁面應該是正確的習俗404.我們如何做到這一點?如何檢查URL是否正確

If a page with .aspx is requested that does not exist, 
to redirect it to the custom 404 page? 

我試圖像(但它只是一個猜測),並沒有工作..

if ((string)System.IO.Path.GetExtension(Request.Path) == string.Empty) 
        { 
         HttpContext.Current.RewritePath("~/custom404.aspx"); 
        } 

謝謝

+1

你爲什麼不使用[ASP .NET的自定義錯誤頁面](http://msdn.microsoft.com/en-us/library/s2f4e3e7.aspx)? – 2011-12-27 23:21:06

回答

0

你明白了什麼,現在當有人進入非存在.aspx頁面?標準的ASP.net頁面未找到錯誤?

通常,可以使用Web.config的customErrors元素。您可以創建的404個狀態碼的特定條目:

<error statusCode="404" redirect="~/Errors/PageNotFound.aspx"/>