Blazor Server 默认的启动 URI 是 /index.html,如果需要更改启动 URI 可以按照以下步骤进行操作:
打开 Startup.cs 文件,在 ConfigureServices() 方法中将 BlazorApplicationBuilder 配置为支持指定启动 URI:
public void ConfigureServices(IServiceCollection services) { services.AddRazorPages(); services.AddServerSideBlazor(); services.Configure(options => { options.DefaultPageStaticFileOptions = new StaticFileOptions { OnPrepareResponse = context => { if (context.File.Name == "index.html") { context.Context.Response.Headers.Add("Set-Cookie", "HttpOnly;Secure;SameSite=Strict"); } } }; }); }
在 Configure() 方法中设置启动 URI:
app.UseEndpoints(endpoints => { endpoints.MapBlazorHub(); endpoints.MapFallbackToPage("/_Host"); endpoints.MapGet("/", async context => { context.Response.Redirect("/new-uri"); await Task.CompletedTask; }); });
上述代码中,在 UseEndpoints() 方法中定义了一个新的路由规则,将根 URI 重定向到 /new-uri,可以根据实际需要修改。
最后,在 index.html 文件中修改对应的 href 属性:
...通过以上几个步骤,即可成功更改 Blazor Server 应用程序的启动 URI。