我有一个使用vs2019和asp.net core 2.2构建的默认Web应用程序。它只设置/ pages(没有MVC控制器和视图)。我的wwwroot(wwwroot / index.html)中有一个页面,当用户浏览到网站的根目录时,我希望该页面作为默认页面。如果我在网址中添加了一个额外的“ /”,但我希望它在没有额外的斜杠的情况下工作,则可以使用以下功能。
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).
AddRazorPagesOptions(options =>
{
options.Conventions.AddPageRoute("", "index.html");
});
}
答案 0 :(得分:1)
只需添加一行app.UseDefaultFiles();
即可:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).
AddRazorPagesOptions(options =>
{
//options.Conventions.AddPageRoute("", "index.html"); // no need
}); ;
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseHttpsRedirection();
app.UseDefaultFiles(); // add this line
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc();
}