为asp.net核心静态文件夹指定默认文件名
我目前有一个生成的index.html,js和其他静态文件居住在一个文件夹中,我将该文件夹标记为静态文件夹(通过在在Startup.cs配置方法:为asp.net核心静态文件夹指定默认文件名
app.UseDefaultFiles();
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new Path.Combine(env.ContentRootPath, @"../build")),
RequestPath = new PathString("/app/")
});
有没有一种方法来设置index.html作为本* /应用程序路径默认响应因为现在本地主机:5000 /应用/返回404,而本地主机:5000 /app/index.html返回index.html。
编辑︰我错过了提及,我没有尝试使用app.UseDefaultFiles()像docs中提到的,但它不窝rk对我来说。服务器仍然会返回一个404
设置默认主页给网站的访客时 访问您的网站开始的地方。为了使您的Web应用程序无需用户完全限定URI即可提供默认页面 ,请按以下方式从Startup.Configure调用 UseDefaultFiles扩展方法。
public void Configure(IApplicationBuilder app)
{
app.UseDefaultFiles();
app.UseStaticFiles(); // For the wwwroot folder
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), @"build")),
RequestPath = new PathString("/app")
});
}
UseDefaultFiles之前,必须调用UseStaticFiles服务于 默认文件。
使用此:
public void Configure(IApplicationBuilder app)
{
// Serve my app-specific default file, if present.
DefaultFilesOptions options = new DefaultFilesOptions();
options.DefaultFileNames.Clear();
options.DefaultFileNames.Add("mydefault.html");
app.UseDefaultFiles(options);
app.UseStaticFiles();
}
欲了解更多详情请点击此链接:
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files
and go to section: "Serving a default document"
他有index.html,UseDefaultFiles()默认包含它,不需要添加mydefault.html – Alexan
一位评论在docs澄清它:
Kieren_Johnstone 精选2017年5月22日
“服务默认文档”一节忽略了一些至关重要的信息 。如果将UseStaticFiles配置为处理非根RequestPath的 ,则似乎需要将相同的FileProvider和RequestPath传入UseDefaultFiles和UseStaticFiles。 你不能总是按照本节所述的来调用它。
因此,这意味着,你应该写这样的事情,使你指定的文件夹提供一个默认的页面:
app.UseDefaultFiles(new DefaultFilesOptions()
{
FileProvider = new Path.Combine(env.ContentRootPath, @"../build")),
RequestPath = new PathString("/app/")
});
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new Path.Combine(env.ContentRootPath, @"../build")),
RequestPath = new PathString("/app/")
});
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files#serving-a-default-document – Alexan