ASP.NET MVC 4 - 异常处理不工作 [英] ASP.NET MVC 4 - Exception Handling Not Working

查看:98
本文介绍了ASP.NET MVC 4 - 异常处理不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在ASP.NET MVC应用4时发生错误,我想自定义视图根据错误的类型用户。例如,找不到网页或发生异常(有有关异常的一些人性化的细节)。我已经检查了如何做到这一点的计算器和其他在线资源的其他例子,但是没有答案都为我工作。

When an error occurs in my ASP.NET MVC 4 application, I would like to customize a view for the user depending on the type of error. For example, page not found or an exception has occurred (with some user-friendly details about the exception). I've checked other examples of how to do this on StackOverflow and other online sources, but none of the answers are working for me.

基本[的HandleError]属性似乎没有在VS2012是工作与MVC 4应用程序针对.NET 4.5。这里是code我在我家的控制器:

The basic [HandleError] attribute does not seem to be working in VS2012 with an MVC 4 application targeting .NET 4.5. Here is the code I have in my home controller:

[HandleError]
public ActionResult Index()
{
    Response.TrySkipIisCustomErrors = true; //doesn't work with or without this
    throw new NullReferenceException("Uh oh, something broke.");
}

这仅仅是抛出一个异常,而我期望默认的〜/共享/ Error.cshtml视图,因为[的HandleError]属性被退回,但我得到的是一个HTTP表明页面500内部服务器错误不能显示。我检查了我的web.config,和不同的配置似乎表现怪异。在节中,目前包含:

It is just throwing an exception, and I would expect the default ~/Shared/Error.cshtml view to be returned because of the [HandleError] attribute, but all I get is an HTTP 500 Internal Server Error indicating that the page could not be displayed. I checked my web.config, and different configurations seem to be behaving weird. In the section, it currently contains:

<customErrors mode="On" />

(我尝试添加的defaultRedirect和模式的customErrors =关,以及,但没有任何效果......无论是共享的错误观点或CustomError认为我有被渲染。如果我改变的customErrors模式关闭,然后我可以看到异常的详细信息如预期,因此它被扔嗯哦,爆出的东西异常正确。

(I've tried adding defaultRedirect and with customErrors mode="Off" as well but that didn't have any effect... neither the shared Error view or the CustomError view I have is being rendered. If I change customErrors mode to off, then I can see the exception details as expected, so it is throwing the "Uh oh, something broke" exception properly.

我也尝试添加onException的处理程序HomeController的,虽然我可以通过调试和看到onException的事件被提出,它没有任何区别:

I've also tried adding an OnException handler to the HomeController, and although I can debug through and see that the OnException event is being raised, it doesn't make any difference:

protected override void OnException(ExceptionContext filterContext)
{
    base.OnException(filterContext);
    filterContext.ExceptionHandled = true;
    if (filterContext == null)
    {
        filterContext.Result = View("CustomError");
        return;
    }
    Exception e = filterContext.Exception;
    // TODO: Log the exception here
    ViewData["Exception"] = e; // pass the exception to the view
    filterContext.Result = View("CustomError");
}

我也试图改变[的HandleError]指定一个观点,但似乎并不要么做什么:

I have also tried changing [HandleError] to specify a view, but that doesn't seem to do anything either:

[HandleError(View="CustomError")]

任何帮助将是非常美联社preciated。请让我知道如果你需要任何更多的细节。

Any help would be much appreciated. Please let me know if you need any more details.

推荐答案

我似乎记得,你必须从一个非本地主机的IP地址(通常在另一台计算机)调用的页面。而且它必须是一个基于IIS服务器,而不是内置的开发服务器(所以IIS或IIS防爆preSS,但你必须配置IIS防爆preSS外部访问,这是一种痛苦)。

I seem to recall that you had to call the page from a non-localhost IP address (typically another computer). And it has to be an IIS based server, not the built-in development server (so IIS or IIS Express, but then you have to configure IIS Express for external access, which is a pain).

您其实可以调试它,你必须要调试的计算机上配置本地服务器接受外部请求,然后从远程服务器调用本地服务器。

You can in fact debug it, you have to configure your local server on your debug computer to accept external requests, then call your local server from the remote server.

这篇关于ASP.NET MVC 4 - 异常处理不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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