Share via


ASP0016:請勿從 RequestDelegate 傳回值

規則識別碼 ASP0016
類別 使用方式
修正程式是中斷或非中斷 不中斷

原因

用來建立 RequestDelegate 的方法傳回 Task<T>RequestDelegate 會捨棄此值。

檔案描述

請勿將提供的值 Delegate 傳回給預期 RequestDelegate 的 API。 例如,下列範例會傳回 Task<string>,其中將會捨棄 Taskstring 值。

var app = WebApplication.Create();
app.Use(next =>
{
    return new RequestDelegate((HttpContext context) =>
    {
        return Task.FromResult(""hello world"");
    });
});

如何修正違規

若要修正此規則的違規,請將傳回型別變更為非泛型 Task,或者,如果委派是路由處理常式,請將它轉換成 Delegate,使得傳回值會寫入回應。

隱藏警告的時機

請勿隱藏此規則的警告。