问题
Hi guys I am trying to create an ASP.net Core 2.0 web application; I have placed the static files in a folder called Content.
I am giving the following path:
<link href="@Url.Content("../Content/css/style.css")" rel="stylesheet">
I am getting a 404 - not found error when loading the view. However the GET request path is correct and the file exists in the same path. This is the path to which the GET request is being done:
http://localhost:49933/Content/css/style.css
I found solutions that requires me to change settings in web.config file, But .Net Core 2.0 does not have one. I have used MVC. Isn't web.config file very important for IIS?
回答1:
You should place the static files you wish to expose under wwwroot
and then reference them accordingly e.g.
See Static files in ASP.NET Core
<link rel="stylesheet" href="~/css/style.css" asp-append-version="true" />
If you want to serve static files outside of the wwwroot
then you will need to configure static file middleware as follows:
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles(); // For the wwwroot folder
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "Content")),
RequestPath = "/Content"
});
}
You can then use similar markup to what you have currently:
<link href="@Url.Content("~/Content/css/style.css")" rel="stylesheet">
See Serve files outside of web root for more information.
来源:https://stackoverflow.com/questions/52405427/asp-net-core-2-0-static-files-not-found-error