4

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>
General Grievance
  • 4,555
  • 31
  • 31
  • 45
FranB
  • 651
  • 1
  • 6
  • 9

2 Answers2

5

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" />

Ryan
  • 19,118
  • 10
  • 37
  • 53
0

It is happening because environment webroot is different than the css folder is currently in. You have to change the webroot or move your css folder to current webroot. Normally the wwwroot folder is set at environment webroot

prisar
  • 3,041
  • 2
  • 26
  • 27
  • Thank you, where can I change the webroot, is it under the properties of the project itself? – FranB Feb 24 '19 at 17:30