asp.net core设置默认起始页Default Page和重定向

用Asp.Net Core的网站Deploy至IIS,因此找了下,发现两种方法:

添加引用

using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Logging;
  • 1.设置默认起始页

  • 2.重定向到起始页

1.设置默认起始页

startup.cs中的Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory logger)方法中加入:

            DefaultFilesOptions options = new DefaultFilesOptions();
            options.DefaultFileNames.Add("index.html");    //将index.html改为需要默认起始页的文件名.
            app.UseDefaultFiles(options);
            app.UseStaticFiles();

2.重定向到起始页

同样在startup.cs中的Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory logger)方法中加入:

            app.UseStaticFiles();
            app.Run(ctx =>
            {
                 ctx.Response.Redirect("/swagger/"); //可以支持虚拟路径或者index.html这类起始页.
                 return Task.FromResult(0);
             });

参考: