ASP.NET中MVC使用AJAX调用JsonResult方法并返回自定义(2)


<span>public<span> <span>class<span> <span>JsonExceptionFilterAttribute<span> <span>:<span> <span>FilterAttribute<span>,<span> <span>IExceptionFilter<span>
<span>{<span>
    <span>public<span> <span>void<span> <span>OnException<span>(<span>ExceptionContext<span> filterContext<span>)<span>
    <span>{<span>
        <span>if<span> <span>(<span>filterContext<span>.<span>RequestContext<span>.<span>HttpContext<span>.<span>Request<span>.<span>IsAjaxRequest<span>())<span>
        <span>{<span>
            filterContext<span>.<span>HttpContext<span>.<span>Response<span>.<span>StatusCode<span> <span>=<span> <span>500<span>;<span>
            filterContext<span>.<span>ExceptionHandled<span> <span>=<span> <span>true<span>;<span>
            filterContext<span>.<span>Result<span> <span>=<span> <span>new<span> <span>JsonResult<span>
            <span>{<span>
                <span>Data<span> <span>=<span> <span>new<span>
                <span>{<span>
                    errorMessage <span>=<span> filterContext<span>.<span>Exception<span>.<span>Message<span>
                <span>},<span>
                <span>JsonRequestBehavior<span> <span>=<span> <span>JsonRequestBehavior<span>.<span>AllowGet<span>
            <span>};<span>
        <span>}<span>
 
    <span>}<span>
<span>}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

之后,我们要给FuckJson()方法加上这个“特征”(Attribute在C#中最好不要翻译为属性),然后故意抛个错做测试。修改后的方法如下:

复制代码 代码如下:


<span>[<span>JsonExceptionFilterAttribute<span>]<span>
<span>public<span> <span>JsonResult<span> <span>FuckJson<span>()<span>
<span>{<span>
    <span>try<span>
    <span>{<span>
        <span>throw<span> <span>new<span> <span>Exception<span>(<span>"oh shit!"<span>);<span>
        <span>return<span> <span>new<span> <span>JsonResult<span>()<span>
        <span>{<span>
            <span>Data<span> <span>=<span> <span>new<span> <span>List<span><string><span>()<span> <span>{<span> <span>"fuck"<span>,<span> <span>"shit"<span> <span>},<span> <span>JsonRequestBehavior<span> <span>=<span> <span>JsonRequestBehavior<span>.<span>AllowGet<span>
        <span>};<span>
    <span>}<span>
    <span>catch<span> <span>(<span>Exception<span> ex<span>)<span>
    <span>{<span>
        <span>throw<span> ex<span>;<span>
    <span>}<span>
<span>}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

捕获异常后,你也可以先记一下log、或是根据你自己的需要处理一下这个异常,然后再throw。

最后我们的AJAX请求代码也得做修改,增加一个error的处理:

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjzgdw.html