Custom API response HTTP status codes with ABP Framework

最后都变了- 提交于 2019-12-22 09:50:03

问题


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

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.

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!
    }

回答1:


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>());


来源:https://stackoverflow.com/questions/38219222/custom-api-response-http-status-codes-with-abp-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!