在传统 ASP.NET 程序中,咱们能够经过配置 IIS 的“URL 重写”功能实现将 HTTP 请求重定向为 HTTPS 。可是该方法在 ASP.NET Core 应用中再也不工做。在 ASP.NET Core 应用中,咱们能够经过一个名为 URL Rewriting 的中间件实现该功能。 首先,请确保项目已经引用了 Microsoft.AspNetCore.Rewrite 包,若是没有,能够经过 nuget 管理器添加引用。接下来只须要在 Startup.cs 文件的 Configure 方法中加入如下代码便可:html
var options = new RewriteOptions() .AddRedirectToHttpsPermanent(); app.UseRewriter(options);
Notice:以上代码来自 Microsoft.AspNetCore.Rewrite 命名空间。c#