为几个HttpStatusCodes设置一个ProducesResponseType typeof [英] Set one ProducesResponseType typeof for several HttpStatusCodes

查看:449
本文介绍了为几个HttpStatusCodes设置一个ProducesResponseType typeof的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置ProducesResponseType以便使用Swagger进行记录.

I am setting the ProducesResponseType so as to have it documented with Swagger.

如果响应成功(确定=> 200),那么它将生成IEnumerable.

If the response is succesful (OK => 200), then it produces a IEnumerable.

[ProducesResponseType(typeof(IEnumerable<MyModel>), 200)]

但是当我遇到异常时,我捕获了异常,然后生成了我的自定义APIError类的对象.当我填充对象时,我可以设置不同的HttpStatusCodes,但是最后,我想要设置的是ProducesResponseType是所有其余HttpStatusCodes的APIError类.我的意思是,我可以获取BadRequest,ServerInternalError,Forbidden等,但是它们都将具有相同的响应类型(ApiError).有什么办法为所有错误的http代码设置这一行代码吗?还是我需要一个一个地设置它?

But when I get an exception, I catch it, and then generate an object of my custom APIError class. When I populate the object, I can set different HttpStatusCodes, but in the end, what I want is to be able to set that ProducesResponseType is my APIError class for all the remaining HttpStatusCodes. I mean, I can get BadRequest, ServerInternalError, Forbidden, etc, but all are going to have the same response type (ApiError). Is there any way to set this line of code for all the error http codes? Or do I need to set it one by one?

[ProducesResponseType(typeof(ApiError), ???)]

最终结果应如下所示:

[ProducesResponseType(typeof(IEnumerable<MyModel>), 200)]
[ProducesResponseType(typeof(ApiError), AllFailureHttpCodes)]

推荐答案

我担心这是不可能的,至少不是每个动作都可以.

I fear that's not possible, at least not per action.

IApiResponseMetadataProvider(请参阅

The IApiResponseMetadataProvider (see here) only has a single int StatusCode { get; } property defined and it's used for both ProducesAttribute and ProducesResponseTypeAttribute.

但是,在特殊情况下,您可以将其注册为全局过滤器,因为无论是GET,POST,PUT还是DELETE,所有操作的错误结果都应该相同.

However, in your special case you may be able to register it as a global filter though, since the error result should be same on all actions no matter if it's GET, POST, PUT or DELETE.

services.AddMvc(options =>
{
    options.Filters.Add(new Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttribute(typeof(ApiError), 400));
    options.Filters.Add(new Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttribute(typeof(ApiError), 401));
    options.Filters.Add(new Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttribute(typeof(ApiError), 409));
});

然后将这些应用于您的MVC/WebAPI应用程序中的每个操作.

These should then be applied to every action in your MVC/WebAPI application.

但是,我个人认为这种用法是错误的,因为这表明您的每种方法都可以处理这些类型的错误.例如409是专门为冲突而设计的,在您使用的WebAPI中,当更新资源时出现错误时,即在使用开放式并发时,记录被另一个用户更改,因此记录的版本已更改且更新失败

However, personally I find this usage wrong, because it would signal that every of your method can handle these type of errors. For example 409 is specifically for conflict, in a WebAPI you use it, when there is an error with updating an resource i.e. when using optimistic concurrency, the record was changed by another user and, hence the version of the record was changed and update failed.

将它们添加到每种方法中都是错误的.仅当您的操作确实处理了此状态码时,才应添加状态码.例如,409仅适用于PUT和POST方法. 404可能适合GET和PUT(更新),但不太适合POST(插入).

Adding these to every method is just wrong. You should only add a status code if your action really handles this status code. For example, 409 is only suitable with PUT and POST Methods. Where 404 may be suitable for GET and PUT (update), but less suitable for POST (inserts).

这特别糟糕,当您使用Swagger等工具生成Api文档时,只会得到错误的文档.您应该将ProducesResponseType视为文档的形式,因此,不应为此使用太笼统的方法.

This is especially bad, when you use tools such as Swagger to generate Api Docs, then you will just get a wrong documentation. You should look at ProducesResponseType as form of documentation and as such, shouldn't use a too generic approach for it.

使用ASP.NET Core 2.2,大大改善了Swagger API支持,其中还包括

With ASP.NET Core 2.2 the Swagger API support was greatly improved, this also includes convention based response type and codes.

它已经带有一组不错的默认约定,但是您也可以覆盖它并提供自己的约定方法.

It already comes with a good set of default conventions, but you can also override it and provide your own convention methods.

这篇关于为几个HttpStatusCodes设置一个ProducesResponseType typeof的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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