Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css file not found - asp.net core web application

I have created an asp.net core web application (I started with the Empty option) and I am building it step by step. It seems my css file is not being read or found though.

When I launch the application I can see that my html page does not look as it should be and when I use the developer tools in edge under the console there is an HTTP404: NOT FOUND - The server has not found anything matching the requested URI (Uniform Resource Identifier). GET - http://localhost:8888/css/site.css error which would indicate my css file is indeed not being read or found.

Now, my css file is under a folder called "css" which is in the root of the project. When I say root I mean that I have just created a folder called css and the path is the same of Program.cs or Startup.cs. There is no wwwroot in my project (seems that asp.net core 2.2 has removed it by default?) and the css folder is not there. As follows the code from my html page, I have tried to add the ~ before the "/css" but still nothing.

<head>
    <link href="/css/site.css" rel="stylesheet" />
</head>
like image 864
FranB Avatar asked Sep 14 '25 01:09

FranB


1 Answers

If you would like that static files to be served outside of the web root (for example in a folder named MyStaticFiles) and it has the structure like

-YourProject
--MyStaticFiles
---css
----site.css
--Program.cs
--Startup.cs

You could use app.UseStaticFiles, refer to Serve files outside of web root

public void Configure(IApplicationBuilder app)
{  
  app.UseStaticFiles(new StaticFileOptions
  {
    FileProvider = new PhysicalFileProvider(
        Path.Combine(Directory.GetCurrentDirectory(), "MyStaticFiles")),
    RequestPath = "/StaticFiles"
  });
}

Then you could get the css from http://localhost:8888/StaticFiles/css/site.css

or <link rel="stylesheet" href="~/StaticFiles/css/site.css" />

like image 199
Ryan Avatar answered Sep 17 '25 00:09

Ryan