使用Global.asax文件的自定义404错误并非始终有效 [英] Custom 404 errors using Global.asax file not always working

查看:109
本文介绍了使用Global.asax文件的自定义404错误并非始终有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们当前使用配置文件来抛出404错误页面,但是我们希望将其移至Global.asax文件,因为我们希望能够使用多个404错误页面,具体取决于用户所在的位置.页.

We currently use the config file to throw the 404 error page, however we would like to move this to the Global.asax file as we want to be able to use multiple 404 error pages, depending on where the user is on the page.

我们当前的设置:

<customErrors mode="On" defaultRedirect="GenericErrorPage.htm">
</customErrors>

<httpErrors existingResponse="Replace" defaultResponseMode="Redirect" errorMode="Custom">
    <remove statusCode="404"/>
    <error statusCode="404" responseMode="Redirect" path="/PageNotFound.aspx"/>
</httpErrors>

我们在Global.asax文件的Application_Error中添加了statusCode检查.

We have added the statusCode check in the Application_Error in the Global.asax file.

if (statusCode == 404)
{
    Response.Redirect(String.Format("/PageNotFound.aspx"));
}

但是,当从配置文件中删除HttpErrors时,除非我们在页面中专门抛出404错误代码,否则不会调用Application Error.像我们的新闻部分一样,如果新闻文章不存在,我们将抛出404错误页面,然后转到正确的错误页面(即

However when removing the HttpErrors from the config file, the Application Error is not called, unless we specifically throw a 404 error code in the page. Like our news section, if the news article doesn't exists, we throw the 404 error page, and it goes to the correct error page (ie. http://[website].com/article?article=blah - This article does not exists).

我已经尝试了多种方法,但是它总是显示IIS 404错误页面,而在键入错误的页面名称(即

I've tried various things, however it always shows the IIS 404 error page, and the not the custom one when just typing in the wrong page name (ie. http://[website].com/blah - This page does not exists) . Trying to debug it, the breakpoint in the Application_Error in the Global.asax file is never hit.

我们需要使Application_Errors工作,以便在您转到不存在的页面时也可以调用它.

We need to get the Application_Errors working so that it is also called when you go to a page that does not exists.

推荐答案

看看这个: https://msdn.microsoft.com /en-us/library/24395wz3%28v=vs.140%29.aspx#robustProgrammingToggle

它基本上说如果页面不存在,它将无法捕获该页面.您将必须在IIS中创建一个自定义处理程序.因此,我认为这可以解释为什么它不进入Application_Error方法.

It basically says that if it is non-existent page it will not catch it. You will have to create a custom handler in IIS. So I think that explains why it is not stepping into the Application_Error method.

在IIS中设置自定义错误处理程序: https://technet.microsoft.com/zh-CN/library/cc753103%28v=ws.10%29.aspx

Setting up a custom error handler in IIS: https://technet.microsoft.com/en-us/library/cc753103%28v=ws.10%29.aspx

这篇关于使用Global.asax文件的自定义404错误并非始终有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆