将 index.html 设置为 asp.net 核心中的默认页面
Setting index.html as default page in asp.net core
如何从我的 wwwroot 中获取 asp.net 核心来为 index.html 文件提供服务?
我想这样做的原因是因为我正在使用 angular CLI 开发一个 angular 4 应用程序,它负责整个构建过程。我已将其设置为构建到我的 asp.net 核心项目的 wwwroot 目录中,但 asp.net 核心不想为它提供服务。
起初我试图通过控制器 return html 文件。我试过这条路线:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}");
});
然后在控制器中我 return html 文件是这样的:
public IActionResult Index()
{
var webRoot = _env.WebRootPath;
var path = System.IO.Path.Combine(webRoot, "index.html");
return File(path, "text/html");
}
这没有用。它 returned 一个 404 not found 异常并给出了路径,但它给出的路径是 index.html 文件的正确路径(我将其剪切并粘贴到资源管理器中并打开了文件)。
我也在启动时声明了这些:
app.UseStaticFiles();
app.UseDefaultFiles();
然后我尝试删除默认路由。现在我可以访问 index.html 文件,但前提是我输入文件名,即:
localhost:58420/index.html
如果我在未指定 "index.html" 的情况下尝试访问域的根目录,我会收到 404 错误。
将 index.html 引用为默认页面的正确方法是什么?我猜想从控制器做这件事可能会更好,因为这样它将与 angular 路由兼容而无需重写。
我需要在 UseStaticFiles() 之前声明 UseDefaultFiles()。
app.UseDefaultFiles();
app.UseStaticFiles();
您正在混合使用 MVC 和默认文件服务 (useDefaultFiles)。
从您的代码中注释掉以下行
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}");
});
并且仅使用 app.UseDefaultFiles();
。它会开始工作。
return File(System.IO.File.OpenRead(Path.Combine(HostingEnvironment.WebRootPath + "/index.html")), "text/html");
一定要帮到你
2022 年 2 月更新
要提供静态文件(例如 index.html),这些文件应位于项目的文件夹 wwwroot
中。如果要将其更改为其他文件夹,请使用 UseWebRoot
。
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseFileServer();
}
此方法还接受默认情况下禁用的 FileServerOptions instance for even more fine tuned control such as enabling directory browsing。
参考:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files
原始答案(2018 年 1 月)
安装 NuGet 包 Microsoft.AspNetCore.StaticFiles。
现在,在 Startup.Configure
方法中,添加:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// Serve the files Default.htm, default.html, Index.htm, Index.html
// by default (in that order), i.e., without having to explicitly qualify the URL.
// For example, if your endpoint is http://localhost:3012/ and wwwroot directory
// has Index.html, then Index.html will be served when someone hits
// http://localhost:3012/
//
// (Function 1)
app.UseDefaultFiles();
// Enable static files to be served. This would allow html, images, etc. in wwwroot
// directory to be served.
//
// (Function 2)
app.UseStaticFiles();
}
注意:调用这些函数的顺序很重要。在 OO 编程中,很难不依赖于排序,因为对象维护的状态在对象的生命周期内可能会发生变化。 (您猜对了,防止此类设计的一种解决方案是实现不变性。)
您现在应该从 wwwroot
目录获取文件(如果您想将其更改为其他目录,请使用 UseWebRoot
)。
来源:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files
只需在 startup.cs
中使用它:
app.UseFileServer();
shorthand 用于:
app.UseDefaultFiles();
app.UseStaticFiles();
它 避免了必须按 正确顺序 (如上所示)
的问题
app.UseDefaultFiles(new DefaultFilesOptions {
DefaultFileNames = new List<string> { "index.html" }
});
app.UseStaticFiles();
这是最佳选择,因为 UseDefaultFiles
URL 重写器将只搜索 index.html
,而不搜索遗留文件:default.htm
、default.html
、和 index.htm
.
// 默认情况下 ASP.net 核心不提供静态文件,例如 HTML、css、图像等...
我需要在请求处理管道中配置中间件以提供静态文件。
// This code would be written in Startup.cs class for configuring the middleware components.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDefaultFiles(); // This sets the default page redirection for the in-comming request
app.UseStaticFiles(); // This serves the static files to the client.
}
在startup.cs里面的configureservices方法中应用这个。这里我们创建一个 DefaultFilesOption 对象,然后清除路径中设置的所有默认文件。接下来,我们添加要设置为默认文件的路径。然后我们使用“app.UseDefaultFiles(defaultfileoptions) 注入依赖项。另外,我们需要注入静态文件作为依赖。
`
public void Configure(IApplicationBuilder app, IWebHostEnvironment env){
DefaultFilesOption defaultFileOptions = new DefaultFilesOption();
defaultFileOptions.DefaultFileNames.Clear();
defaultFilesOptions.DefaultFileNames.Add("Index.html");
app.UseDefaultFiles(defaultFileOptions);
app.UseStaticFiles();
}
`
如何从我的 wwwroot 中获取 asp.net 核心来为 index.html 文件提供服务?
我想这样做的原因是因为我正在使用 angular CLI 开发一个 angular 4 应用程序,它负责整个构建过程。我已将其设置为构建到我的 asp.net 核心项目的 wwwroot 目录中,但 asp.net 核心不想为它提供服务。
起初我试图通过控制器 return html 文件。我试过这条路线:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}");
});
然后在控制器中我 return html 文件是这样的:
public IActionResult Index()
{
var webRoot = _env.WebRootPath;
var path = System.IO.Path.Combine(webRoot, "index.html");
return File(path, "text/html");
}
这没有用。它 returned 一个 404 not found 异常并给出了路径,但它给出的路径是 index.html 文件的正确路径(我将其剪切并粘贴到资源管理器中并打开了文件)。
我也在启动时声明了这些:
app.UseStaticFiles();
app.UseDefaultFiles();
然后我尝试删除默认路由。现在我可以访问 index.html 文件,但前提是我输入文件名,即:
localhost:58420/index.html
如果我在未指定 "index.html" 的情况下尝试访问域的根目录,我会收到 404 错误。
将 index.html 引用为默认页面的正确方法是什么?我猜想从控制器做这件事可能会更好,因为这样它将与 angular 路由兼容而无需重写。
我需要在 UseStaticFiles() 之前声明 UseDefaultFiles()。
app.UseDefaultFiles();
app.UseStaticFiles();
您正在混合使用 MVC 和默认文件服务 (useDefaultFiles)。 从您的代码中注释掉以下行
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}");
});
并且仅使用 app.UseDefaultFiles();
。它会开始工作。
return File(System.IO.File.OpenRead(Path.Combine(HostingEnvironment.WebRootPath + "/index.html")), "text/html");
一定要帮到你
2022 年 2 月更新
要提供静态文件(例如 index.html),这些文件应位于项目的文件夹 wwwroot
中。如果要将其更改为其他文件夹,请使用 UseWebRoot
。
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseFileServer();
}
此方法还接受默认情况下禁用的 FileServerOptions instance for even more fine tuned control such as enabling directory browsing。
参考:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files
原始答案(2018 年 1 月)
安装 NuGet 包 Microsoft.AspNetCore.StaticFiles。
现在,在 Startup.Configure
方法中,添加:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// Serve the files Default.htm, default.html, Index.htm, Index.html
// by default (in that order), i.e., without having to explicitly qualify the URL.
// For example, if your endpoint is http://localhost:3012/ and wwwroot directory
// has Index.html, then Index.html will be served when someone hits
// http://localhost:3012/
//
// (Function 1)
app.UseDefaultFiles();
// Enable static files to be served. This would allow html, images, etc. in wwwroot
// directory to be served.
//
// (Function 2)
app.UseStaticFiles();
}
注意:调用这些函数的顺序很重要。在 OO 编程中,很难不依赖于排序,因为对象维护的状态在对象的生命周期内可能会发生变化。 (您猜对了,防止此类设计的一种解决方案是实现不变性。)
您现在应该从 wwwroot
目录获取文件(如果您想将其更改为其他目录,请使用 UseWebRoot
)。
来源:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files
只需在 startup.cs
中使用它:
app.UseFileServer();
shorthand 用于:
app.UseDefaultFiles();
app.UseStaticFiles();
它 避免了必须按 正确顺序 (如上所示)
的问题app.UseDefaultFiles(new DefaultFilesOptions {
DefaultFileNames = new List<string> { "index.html" }
});
app.UseStaticFiles();
这是最佳选择,因为 UseDefaultFiles
URL 重写器将只搜索 index.html
,而不搜索遗留文件:default.htm
、default.html
、和 index.htm
.
// 默认情况下 ASP.net 核心不提供静态文件,例如 HTML、css、图像等... 我需要在请求处理管道中配置中间件以提供静态文件。
// This code would be written in Startup.cs class for configuring the middleware components.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDefaultFiles(); // This sets the default page redirection for the in-comming request
app.UseStaticFiles(); // This serves the static files to the client.
}
在startup.cs里面的configureservices方法中应用这个。这里我们创建一个 DefaultFilesOption 对象,然后清除路径中设置的所有默认文件。接下来,我们添加要设置为默认文件的路径。然后我们使用“app.UseDefaultFiles(defaultfileoptions) 注入依赖项。另外,我们需要注入静态文件作为依赖。
`
public void Configure(IApplicationBuilder app, IWebHostEnvironment env){
DefaultFilesOption defaultFileOptions = new DefaultFilesOption();
defaultFileOptions.DefaultFileNames.Clear();
defaultFilesOptions.DefaultFileNames.Add("Index.html");
app.UseDefaultFiles(defaultFileOptions);
app.UseStaticFiles();
}
`