Search code examples
c#asp.net-corehttpscompiler-errorsasp.net-core-2.1

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


I have a .NET Core 2.1 application and want to use HTTPs according to this article. So I added

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

to the ConfigureServices(...) method and

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

to the Configure(...) method. I checked the namespace of the methods and they should be present in Microsoft.AspNetCore.Builder (e.g. UseHsts()), but all of them generate the compiler error

'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?)

or

'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?)

Am I missing a using directive or a specific NuGet package? How can I use those methods?


Solution

  • Most dependencies of ASP.NET Core are contained in the Microsoft.AspNetCore.App meta package, which also includes the extensions methods from the question. After adding this package, the methods should become available.

    The Microsoft.AspNetCore.App package is automatically included by the IDE when creating a new project, but may be missing when upgrading or converting an existing project.