使用ABP框架的自定义API响应HTTP状态代码 [英] Custom API response HTTP status codes with ABP Framework

查看:638
本文介绍了使用ABP框架的自定义API响应HTTP状态代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过ASP.NET Boilerplate返回自定义HTTP状态代码(如4xx)?

Is there any way to return custom HTTP Status Codes (like 4xx) with ASP.NET Boilerplate?

我想在验证的上下文中设置自定义的应用程序特定的HTTP代码,以增加粒度.目前ABP会为所有验证错误设置200(确定).

I would like to set custom application specific HTTP codes in context of validation to add more granularity. Currently ABP would set 200(OK) for all validation errors.

在ABP源代码中,很少看到类似下面的地方,其中的Response.StatusCode是由像这样的框架设置的:

In ABP source code is see few places like one below where Response.StatusCode is set by the framework like here :

    private void HandleAndWrapException(ExceptionContext context)
    {
        if (!ActionResultHelper.IsObjectResult(context.ActionDescriptor.GetMethodInfo().ReturnType))
        {
            return;
        }

        context.HttpContext.Response.Clear();
        context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
        context.Result = new ObjectResult(
            new AjaxResponse(
                _errorInfoBuilder.BuildForException(context.Exception),
                context.Exception is AbpAuthorizationException
            )
        );

        context.Exception = null; //Handled!
    }

推荐答案

我最终所做的是完全替换了ABP异常过滤器.在我的自定义过滤器中,我可以设置所需的任何状态代码.

What I eventually did was replace ABP exception filter altogether. In my custom filter I could set any status code I need.

var filters = Configuration.Modules.AbpWebApi().HttpConfiguration.Filters;
var currentExceptionFilter = filters
    .First(h => h.Instance is AbpExceptionFilterAttribute).Instance;

filters.Remove(currentExceptionFilter );
filters.Add(IocManager.Resolve<MyApiExceptionFilter>());

这篇关于使用ABP框架的自定义API响应HTTP状态代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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