什么是最好的通过AJAX在MVC中异常处理最简单的方法? [英] What is the best & easiest way for Exception Handling via AJAX in MVC?

查看:125
本文介绍了什么是最好的通过AJAX在MVC中异常处理最简单的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC5项目,需要使用可以用于所有Controller或Action方法的自定义方法来处理异常。为了解决这个问题,我发现了一些例子,贴在 ASP.NET MVC中的异常处理,并尝试使用如下所示的方法:



自定义属性

  public class MyErrorHandlerAttribute:FilterAttribute,IExceptionFilter 
{
public void OnException(ExceptionContext filterContext)
{
filterContext.ExceptionHandled = true;
filterContext.Result = new JsonResult
{
Data = new {success = false,error = filterContext.Exception.ToString()},
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
}

控制器: p>

  [MyErrorHandler] 
public ActionResult Delete(int id)
{
Course deletedCourse = repository。 DeleteCourse(ID);
if(deletedCourse == null)
{
throw new Exception(Error ...);
}
}

查看: p>

  $。ajax({

//为省略省略省略

成功:function(result,textStatus,XMLHttpRequest){
if(!result.success){
alert(result.error);
}
}
});

尽管这种方法工作正常,但是filterContext中没有足够的信息返回到View有意义的消息或异常类型,即数据库约束错误等那么,有没有更好的方法有一个关于异常和使用JSON的详细信息在这个例子。

解决方案

ExceptionContext.Exception将包含抛出的异常,如果没有足够的信息,那么可能是因为您没有提供它(基于示例代码,仅提供异常消息)。



处理应用程序级过滤器中的所有异常情况听起来很棒,直到有特殊情况需要解决。并非所有错误都创建相同。例外情况也很贵,最好考虑从控制器动作返回有意义的错误对象,并使用适当的HTTP响应代码来识别问题。



我确定您已经意识到,将完整的异常消息,调用堆栈等发送到浏览器可能不明智,因为它可以揭示应用程序的设计和结构,可能允许攻击者识别漏洞。



发送原始异常数据后,向通常无法做某事的用户提供错误:用户。我强烈建议您记录异常,然后向用户发送有意义的消息,但不会泄露您的应用的敏感信息。


I have an MVC5 project and need to handle exceptions by using a custom method that can be used for all of the Controller or Action methods. For solving the problem I have found some example as posted on Exception handling in ASP.NET MVC and tried to use follow an approach as shown below:

Custom Attribute:

public class MyErrorHandlerAttribute : FilterAttribute, IExceptionFilter
{
    public void OnException(ExceptionContext filterContext)
    {
        filterContext.ExceptionHandled = true;
        filterContext.Result = new JsonResult
        {
            Data = new { success = false, error = filterContext.Exception.ToString() },
            JsonRequestBehavior = JsonRequestBehavior.AllowGet
        };
    }
}

Controller:

[MyErrorHandler]
public ActionResult Delete(int id)
{
    Course deletedCourse = repository.DeleteCourse(id);             
    if (deletedCourse == null)
    {
        throw new Exception("Error...");
    }
}

View:

$.ajax({

    //code omitted for brevity

    success: function (result, textStatus, XMLHttpRequest) {
        if (!result.success) {
            alert(result.error);
        }
    }
});

Although this approach is working properly, there is not enough information in filterContext to return to the View as an meaningful message or exception type i.e. "database constraint error, etc." So, is there any better approach having a detailed information regarding to exception and using JSON as in this example.

解决方案

ExceptionContext.Exception will contain the thrown exception, if there is not enough information in that, then it could be because you are not providing it (based on the example code, only the exception message is provided).

Handling all exceptions in an application-level filter sounds great until you have special cases that you need to address. Not all errors are created equal. Exceptions are also expensive, it may be better to consider sending meaningful error objects back from the controller action, with an appropriate HTTP Response Code identifying the issue.

I'm sure you're already aware but surfacing full exception messages, call stacks, etc to the browser may not be wise as it can reveal the design and structure of your application, possibly allowing an attacker to identify a vulnerability.

Sending raw exception data back provides the error to the person who is typically least able to do something about it: the user. I would strongly recommend logging the exception and then sending a meaningful message back to the user, but without potentially divulging sensitive information about your app.

这篇关于什么是最好的通过AJAX在MVC中异常处理最简单的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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