简体   繁体   English

如何在Ajax(Post)请求期间抛出自定义http状态代码

[英]How throw custom http status code during Ajax(Post) request

I need to throw HttpException during AjaxRequest in Controller and CustomFilterAttribute 我需要在ControllerCustomFilterAttribute中的AjaxRequest期间抛出HttpException

When I throw Exception in Controller with 403 error 当我在Controller抛出Exception 403错误

[HttpPost]
[CustomAuthorize]
public ActionResult AjaxSelectBinding()
{
     // 403 Error code
     throw new HttpException((int)HttpStatusCode.Forbidden, "Forbidden");
}

In client script I always get the result code - 500 在客户端脚本中,我总是得到结果代码500

 $.ajax({
            type: 'POST',
            url: '/Groups/AjaxSelectBinding',
            success: function(data) {
            },
            error: function (xhr, ajaxOptions, thrownError) {
                 // HERE I GET ALWAYS 500 ERROR CODE
            }
        });

How can I throw HttpException in my FilterAttribute and get this code in client page. 如何在我的FilterAttribute抛出HttpException并在客户端页面中获取此代码。 I try do this, but I get 200 status code: 我尝试这样做,但我得到200状态代码:

public class CustomAuthorize : AuthorizeAttribute
{
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        base.OnAuthorization(filterContext);

        SharedControllerBase ctrl = (SharedControllerBase)filterContext.Controller;

        if (!ctrl.User.Identity.IsAuthenticated &&
             filterContext.HttpContext.Request.IsAjaxRequest())
        {
            filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
            filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
        }
    }

When I try throw Exception in FilterAttribute I get 500 Status Code again 当我尝试在FilterAttribute抛出Exception ,我再次获得500状态代码

First things first HttpStatusCode.Unauthorized = 401 , not 403 status code. 首先是HttpStatusCode.Unauthorized = 401 ,而不是403状态代码。 There is an important distinction between those 2 codes. 这两个代码之间有一个重要的区别。

When you set the status code to 401 some nasty things happen: you automatically get redirected to the Login page by the ASP.NET Forms authentication module => the login page is served with status code = 200. Phil Haack addressed this issue in the following blog post . 当您将状态代码设置为401时,会发生一些令人讨厌的事情:您通过ASP.NET Forms身份验证模块自动重定向到“登录”页面=>登录页面的状态代码为200. Phil Haack在以下问题中解决了此问题博客文章

As far as throw new HttpException((int)HttpStatusCode.Forbidden, "Forbidden"); throw new HttpException((int)HttpStatusCode.Forbidden, "Forbidden"); is concerned in your controller action, well, you throw an exception that is of type HttpException and whose StatusCode is set to 401 but other than that there is absolutely nothing that will catch this exception and set the corresponding response status code. 关注你的控制器动作,你抛出一个HttpException类型的异常,并且其StatusCode设置为401,但除此之外绝对没有任何东西可以捕获这个异常并设置相应的响应状态代码。 So the exception bubbles up and since you don't presumably have a global exception handler it is translated as a 500 error page by the server. 因此异常起泡,因为您可能没有全局异常处理程序,所以它被服务器翻译为500错误页面。

Here's an example of a global exception handler that you might find useful. 以下是您可能会发现有用的全局异常处理程序的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM