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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為 API 請求禁用 StatusCodePages 中間件

為 API 請求禁用 StatusCodePages 中間件

C#
慕森王 2022-01-09 16:35:28
我正在使用 asp.net core 2.1,StatusCodePagesMiddleware.cs的來源if (!statusCodeFeature.Enabled){    // Check if the feature is still available because other middleware (such as a web API written in MVC) could    // have disabled the feature to prevent HTML status code responses from showing up to an API client.    return;}似乎提出了 API 中間件禁用處理程序的假設(shè),但事實并非如此。是否有一種更簡潔的方法可以僅為 MVC 請求啟用中間件,而無需調(diào)用app.UseWhen和檢查路徑字符串,或者這是最好的方法?app.UseWhen(    context => !context.Request.Path.Value.StartsWith("/api", StringComparison.OrdinalIgnoreCase),    builder => builder.UseStatusCodePagesWithReExecute("/.../{0}"));
查看完整描述

2 回答

?
呼如林

TA貢獻1798條經(jīng)驗 獲得超3個贊

對我來說正確的答案是UseStatusCodePagesWithReExecute在 Startup.cs 中使用 plain ,但在錯誤控制器中改變處理方式。這使我可以返回純文本內(nèi)容以顯示 API 錯誤,但為用戶保留友好的視圖。


啟動.cs


app.UseStatusCodePagesWithReExecute("/error/{0}");

錯誤控制器:


[HttpGet("error/{statusCode:int}")]

public IActionResult Error(int statusCode)

{

    var statusCodeFeature = HttpContext.Features.Get<IStatusCodeReExecuteFeature>();


    var exceptionDataFeature = HttpContext.Features.Get<IExceptionHandlerPathFeature>();


    // ... Other logging and stuff


    IActionResult actionResult;


    if (statusCodeFeature == null || statusCodeFeature.OriginalPath.StartsWith("/api", StringComparison.InvariantCultureIgnoreCase))

    {

        actionResult = Content($"The request could not be processed: {statusCode.ToString(CultureInfo.InvariantCulture)}");

    }

    else

    {

        ViewBag.StatusCode = statusCode;


        actionResult = View();

    }


    return actionResult;

}


查看完整回答
反對 回復(fù) 2022-01-09
?
慕田峪4524236

TA貢獻1875條經(jīng)驗 獲得超5個贊

這在某種程度上取決于解釋,但我會說評論只是暗示某些東西可能會禁用該功能,但默認情況下并不是任何東西實際上會起作用。


我認為沒有任何明顯更清潔的東西 - 你有什么是有道理的,但另一種選擇是使用一個自定義中間件來關(guān)閉該功能。這可能是這樣的:


public void Configure(IApplicationBuilder app)

{

    // ...

    app.UseStatusCodePagesWithReExecute("/.../{0}");


    app.Use(async (ctx, next) =>

    {

        if (ctx.Request.Path.Value.StartsWith("/api", StringComparison.OrdinalIgnoreCase))

        {

            var statusCodeFeature = ctx.Features.Get<IStatusCodePagesFeature>();


            if (statusCodeFeature != null && statusCodeFeature.Enabled)

                statusCodeFeature.Enabled = false;

        }


        await next();

    });


    // ...

    app.UseMvc();

    // ...

}


查看完整回答
反對 回復(fù) 2022-01-09
  • 2 回答
  • 0 關(guān)注
  • 175 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

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

公眾號

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