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>

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

Thank you, where can I change the webroot, is it under the properties of the project itself?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.