[英]Why is my ExceptionFilter not fetching exceptions when the site is deployed?
I have a MVC 4 application and I've registered an Exception filter: 我有一个MVC 4应用程序,并且已经注册了一个异常过滤器:
Global.asax.cs: 的Global.asax.cs:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
// Here my filter gets registered
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
FilterConfig.cs: FilterConfig.cs:
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleUnhandledExceptionFilter());
}
}
HandleUnhandledExceptionFilter.cs: HandleUnhandledExceptionFilter.cs:
public class HandleUnhandledExceptionFilter : IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
filterContext.HttpContext.Response.StatusCode = 500;
filterContext.HttpContext.Response.Write("An unknown error occured");
filterContext.ExceptionHandled = true;
}
}
Notice, I don't use the WEB API Framework, but I use the MVC filters. 注意,我不使用WEB API框架,但使用MVC过滤器。
When an exception occurs, I want to display the text An unknown error occured
. 发生异常时,我想显示文本
An unknown error occured
。 This works fine when I execute the site locally. 当我在本地执行站点时,此方法工作正常。 When I publish the site, copy the published files to the webserver and browse on my machine (which is not the webserver) and get an eception, I dont see the text
An unknown error occured
. 当我发布站点时,将已发布的文件复制到Web服务器并在我的计算机(不是Web服务器)上浏览并收到消息,我看不到
An unknown error occured
的文本。 Instead I get the source of an error page. 相反,我得到了一个错误页面的来源。 So I see literally this:
所以我从字面上看到:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html .... etc.
First I thought maybe it has something to do with customErrors
, so I added a customErrors
tag to my webconfig: 首先,我认为这可能与
customErrors
,因此我在我的webconfig中添加了一个customErrors
标记:
<customErrors mode="Off"></customErrors>
But unfortunately, this didn't do the trick. 但是不幸的是,这并没有解决问题。 So my question is: what do I have to do to show my own error message?
所以我的问题是:显示我自己的错误消息该怎么办?
I found out that my filter is catching exceptions and that the 'real' problem was IIS 7. By default, IIS 7 uses it's own (custom) error pages when the Http StatusCode > 400. So in my case IIS 7 used it's own error page. 我发现我的过滤器正在捕获异常,而“真正的”问题是IIS7。默认情况下,当Http StatusCode> 400时,IIS 7使用它自己的(自定义)错误页面。因此,在我的情况下,IIS 7使用了它自己的错误。页。 Obviously this was not what I wanted.
显然,这不是我想要的。
Two ways of solving this issue are: 解决此问题的两种方法是:
Response.TrySkipIisCustomErrors = true;
Response.TrySkipIisCustomErrors = true;
. ResponseText
. ResponseText
。 web.config
in the system.webServer
element add this line: <httpErrors existingResponse="PassThrough"></httpErrors>
. system.webServer
元素的web.config
中,添加以下行: <httpErrors existingResponse="PassThrough"></httpErrors>
。 For more information about this topic, see What to expect from IIS7 custom error module 有关此主题的更多信息,请参见IIS7自定义错误模块的预期结果
If you just want to display Error occurred
then you can try this 如果您只想显示
Error occurred
则可以尝试此操作
<customErrors mode="RemoteOnly" defaultRedirect="/YOURCONTROLLER/VIEW" />
abovecode will redirect to page which you have mention in defaultredirect
上述代码将重定向到您在
defaultredirect
提到的页面
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.