Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net Core 2.0 Static files not found error

I am trying to create an ASP.net Core 2.0 web application; I have placed the static files in a folder called Content.

enter image description here

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?

like image 673
Abhilash Gopalakrishna Avatar asked Sep 19 '18 12:09

Abhilash Gopalakrishna


People also ask

How do I serve a static file in NET Core?

To serve static files from an ASP.NET Core app, you must configure static files middleware. With static files middleware configured, an ASP.NET Core app will serve all files located in a certain folder (typically /wwwroot).

What is the default directory for static files in ASP.NET Core?

Static files are stored within the project's web root directory. The default directory is {content root}/wwwroot , but it can be changed with the UseWebRoot method.

What are static files in ASP.NET Core?

ASP.NET MVC 5 for Beginners Static files like JavaScript files, images, CSS files that we have on the file system are the assets that ASP.NET Core application can serve directly to clients. Static files are typically located in the web root (wwwroot) folder.

What is use of Wwwroot folder in ASP.NET Core?

By default, the wwwroot folder in the ASP.NET Core project is treated as a web root folder. Static files can be stored in any folder under the web root and accessed with a relative path to that root.


1 Answers

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.

like image 112
SpruceMoose Avatar answered Nov 07 '22 07:11

SpruceMoose