2 回答

TA貢獻(xiàn)1863條經(jīng)驗(yàn) 獲得超2個(gè)贊
您可以創(chuàng)建自定義IApplicationModelProvider并在OnProvidersExecuting方法中添加所需的過(guò)濾器。
ProduceResponseTypeModelProvider.cs
public class ProduceResponseTypeModelProvider : IApplicationModelProvider
{
public int Order => 3;
public void OnProvidersExecuted(ApplicationModelProviderContext context)
{
}
public void OnProvidersExecuting(ApplicationModelProviderContext context)
{
foreach (ControllerModel controller in context.Result.Controllers)
{
foreach (ActionModel action in controller.Actions)
{
// I assume that all you actions type are Task<ActionResult<ReturnType>>
Type returnType = action.ActionMethod.ReturnType.GenericTypeArguments[0].GetGenericArguments()[0];
action.Filters.Add(new ProducesResponseTypeAttribute(StatusCodes.Status510NotExtended));
action.Filters.Add(new ProducesResponseTypeAttribute(returnType, StatusCodes.Status200OK));
action.Filters.Add(new ProducesResponseTypeAttribute(returnType, StatusCodes.Status500InternalServerError));
}
}
}
}
然后你需要將其注冊(cè)到IServiceCollection
啟動(dòng).cs
public void ConfigureServices(IServiceCollection services)
{
...
services.TryAddEnumerable(ServiceDescriptor.Transient<IApplicationModelProvider, ProduceResponseTypeModelProvider>());
...
}

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊
我們也使用這行代碼, 它提供錯(cuò)誤處理、動(dòng)詞屬性檢查以及參數(shù)是否存在,
public class ProduceResponseTypeModelProvider : IApplicationModelProvider
? ? {
? ? ? ? public int Order => 3;
? ? ? ? public void OnProvidersExecuted(ApplicationModelProviderContext context)
? ? ? ? {
? ? ? ? }
? ? ? ? public void OnProvidersExecuting(ApplicationModelProviderContext context)
? ? ? ? {
? ? ? ? ? ? foreach (ControllerModel controller in context.Result.Controllers)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? foreach (ActionModel action in controller.Actions)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? Type returnType = null;
? ? ? ? ? ? ? ? ? ? if (action.ActionMethod.ReturnType.GenericTypeArguments.Any())
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? if (action.ActionMethod.ReturnType.GenericTypeArguments[0].GetGenericArguments().Any())
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? returnType = action.ActionMethod.ReturnType.GenericTypeArguments[0].GetGenericArguments()[0];
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? var methodVerbs = action.Attributes.OfType<HttpMethodAttribute>().SelectMany(x => x.HttpMethods).Distinct();
? ? ? ? ? ? ? ? ? ? bool actionParametersExist = action.Parameters.Any();
? ? ? ? ? ? ? ? ? ? AddUniversalStatusCodes(action, returnType);
? ? ? ? ? ? ? ? ? ? if (actionParametersExist == true)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? AddProducesResponseTypeAttribute(action, null, 404);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? if (methodVerbs.Contains("POST"))
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? AddPostStatusCodes(action, returnType, actionParametersExist);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? public void AddProducesResponseTypeAttribute(ActionModel action, Type returnType, int statusCodeResult)
? ? ? ? {
? ? ? ? ? ? if (returnType != null)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? action.Filters.Add(new ProducesResponseTypeAttribute(returnType, statusCodeResult));
? ? ? ? ? ? }
? ? ? ? ? ? else if (returnType == null)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? action.Filters.Add(new ProducesResponseTypeAttribute(statusCodeResult));
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? public void AddUniversalStatusCodes(ActionModel action, Type returnType)
? ? ? ? {
? ? ? ? ? ? AddProducesResponseTypeAttribute(action, returnType, 200);
? ? ? ? ? ? AddProducesResponseTypeAttribute(action, null, 500);
? ? ? ? }
? ? ? ? public void AddPostStatusCodes(ActionModel action, Type returnType, bool actionParametersExist)
? ? ? ? {
? ? ? ? ? ? AddProducesResponseTypeAttribute(action, returnType, 201);
? ? ? ? ? ? AddProducesResponseTypeAttribute(action, returnType, 400);
? ? ? ? ? ? if (actionParametersExist == false)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? AddProducesResponseTypeAttribute(action, null, 404);
? ? ? ? ? ? }
? ? ? ? }
? ? }
- 2 回答
- 0 關(guān)注
- 391 瀏覽
添加回答
舉報(bào)