我有中间件处理所有未处理的异常和返回
public class ErrorResponseModel {
public string ErrorMessage { get; set; }
}
我喜欢将[ProducesResponseType(typeof(ErrorResponseModel), 500)]
添加到我的所有控制器中,但我不明白如何做一次(没有复制或为我的所有控制器引入基类)。
将其添加为全局过滤器,如下所示:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(o =>
{
o.Filters.Add(new ProducesResponseTypeAttribute(typeof(ErrorResponseModel), 500));
})
}
这与应用于所有控制器基本相同。