2011-12-27 199 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"/>