第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Net Core API:將 ProducesResponseType 設(shè)置為全局參數(shù)或自動(dòng)化

Net Core API:將 ProducesResponseType 設(shè)置為全局參數(shù)或自動(dòng)化

C#
慕運(yùn)維8079593 2023-12-17 20:03:46
我們有 100 多個(gè) API,并且必須為我們所有的 API 在頂部、200 個(gè)、500 個(gè)等編寫(xiě) ProducesResponseType。有沒(méi)有一種方法可以為我們所有的 get 函數(shù)制作這個(gè)全局參數(shù),這樣我們就不必繼續(xù)重復(fù)代碼?嘗試讓API遵循Dry原則,成為瘦控制器。[HttpGet("[Action]/{id}")][ProducesResponseType(typeof(GetBookResponse), StatusCodes.Status200OK)][ProducesResponseType(StatusCodes.Status404NotFound)][ProducesResponseType(typeof(GetBookResponse), StatusCodes.Status500InternalServerError)]public async Task<ActionResult<GetBookResponse>> GetByBook(int id){   var book = await bookservice.GetBookById(id);   return Ok(book);}
查看完整描述

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

    ...

}


查看完整回答
反對(duì) 回復(fù) 2023-12-17
?
慕萊塢森

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

? ? ? ? ? ? }

? ? ? ? }

? ? }


查看完整回答
反對(duì) 回復(fù) 2023-12-17
  • 2 回答
  • 0 關(guān)注
  • 391 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)