指定asp.net核心静态文件夹的默认文件名 [英] Specifying default file name for asp.net core static folder
问题描述
我目前在一个文件夹中有一个生成的index.html,js和其他静态文件,并且将该文件夹标记为静态文件夹(通过在Startup.cs中的Configure方法中添加以下内容:
I currently have a generated index.html, js and other static files living in a folder and I'm marking that folder as a static folder (by adding the following in the Configure method in Startup.cs:
app.UseDefaultFiles();
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new Path.Combine(env.ContentRootPath, @"../build")),
RequestPath = new PathString("/app/")
});
是否可以将index.html设置为此*/app路由的默认响应?因为现在localhost:5000/app/返回404,而localhost:5000/app/index.html返回index.html.
Is there a way to set index.html as the default response for this */app route? Because right now localhost:5000/app/ returns a 404 while localhost:5000/app/index.html returns the index.html.
我错过了提及我确实尝试过使用app.UseDefaultFiles()的情况,如
I missed to mention that I did try using app.UseDefaultFiles() like mentioned in docs but it does not work for me. The server still returns a 404
推荐答案
该文档已对其进行了澄清:
基恩·约翰斯通(Kieren_Johnstone) 精选于2017年5月22日
Kieren_Johnstone Featured May 22, 2017
提供默认文档"部分忽略了一些重要内容 信息.如果您将UseStaticFiles配置为可以正常使用 非根RequestPath,您需要传递相同的FileProvider和 看起来,RequestPath都进入UseDefaultFiles和UseStaticFiles中. 您不能总是像本节所述那样直接调用它.
The section, "Serving a default document" misses some vital information. If you configure your UseStaticFiles to work off a non-root RequestPath, you need to pass the same FileProvider and RequestPath into both UseDefaultFiles and UseStaticFiles, it seems. You can't always just call it as stated in the section.
因此,您应该编写类似这样的内容,以使您指定的文件夹能够提供默认页面:
So that means, you should write something like this to enable your specified folder to provide a default page:
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/")
});
这篇关于指定asp.net核心静态文件夹的默认文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!