.NET Core 2.1:未定义 AddHsts() 和 AddHttpsRedirection()

.NET Core 2.1: AddHsts() and AddHttpsRedirection() not defined

我有一个 .NET Core 2.1 应用程序,想根据 this 文章使用 HTTP。所以我添加了

services.AddHsts(options =>
{
    options.MaxAge = TimeSpan.FromDays(60);
});
services.AddHttpsRedirection(options =>
{
    options.RedirectStatusCode = StatusCodes.Status307TemporaryRedirect;
    options.HttpsPort = 5001;
});

ConfigureServices(...)方法和

app.UseHsts();
app.UseHttpsRedirection();

Configure(...)方法。我检查了方法的命名空间,它们应该出现在 Microsoft.AspNetCore.Builder (e.g. UseHsts()) 中,但它们都会生成编译器错误

'IApplicationBuilder' does not contain a definition for 'UseHsts' and no accessible extension method 'UseHsts' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)

'IServiceCollection' does not contain a definition for 'AddHttpsRedirection' and no accessible extension method 'AddHttpsRedirection' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)

我是否缺少 using 指令或特定的 NuGet 包?我该如何使用这些方法?

ASP.NET Core 的大多数依赖项都包含在 Microsoft.AspNetCore.App 元包中,其中还包括问题中的扩展方法。添加此包后,这些方法应该可用。

Microsoft.AspNetCore.App 包在创建新项目时由 IDE 自动包含,但在 upgrading 或转换现有项目时可能会丢失。