简体   繁体   English

为什么在部署站点时我的ExceptionFilter不能获取异常?

[英]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: 解决此问题的两种方法是:

  1. Set Response.TrySkipIisCustomErrors = true; 设置Response.TrySkipIisCustomErrors = true; . This will tel IIS7 not to use it's own error pages, but to use the ResponseText . 这将使IIS7不再使用它自己的错误页面,而是使用ResponseText
  2. In the 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.

相关问题 ExceptionFilter不处理异常 - ExceptionFilter do not handle exceptions 一旦我添加了自定义 ExceptionFilter,Serilog 就会停止记录异常 - Serilog stops logging exceptions as soon as I add a custom ExceptionFilter 部署MVC站点时Windows身份验证不起作用 - Windows authentication not working when deployed MVC site 部署站点时SignalR消息不起作用 - SignalR messages not working when site is deployed 在云上部署时调用 workItemTrackingHttpClient.CreateWorkItemAsync() 会抛出不同的异常 - Calling workItemTrackingHttpClient.CreateWorkItemAsync() throws different exceptions when deployed on cloud 如何允许自定义ExceptionFilter在异常期间继续调用指定的Action方法 - How to allow custom ExceptionFilter to continue to call the designated Action Method during exceptions 为什么在部署我的应用程序时SQL Server CE无法识别数据库 - Why SQL Server CE does not recognize DB when my app is deployed MVC ExceptionFilter何时与应用程序级错误处理程序一起执行? - When will an MVC ExceptionFilter be executed vs the application-level error handler? 为什么文件在本地Web应用程序中存在,但在将应用程序部署到Web服务器时却不存在? - Why is a file present within my web application locally, but not present when the application is deployed to the web server? 为什么在通过带有Entity Framework 6.x的SqlQuery从View获取数据时不使用ColumnAttribute? - Why my ColumnAttribute are not used when fetching data from a View via SqlQuery with Entity Framework 6.x?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM