'HRef Not Using Stylesheets From New Folder?

I am having an issue with a an MVC ASP .Net project where i am attempting to add a new folder to hold all the css files I plan on using for the future however when testing the file in the folder is seems the project is not able to use them. I have already done the following troubleshooting:

  • Tested the same file in the css folder of the wwwroot properties which worked
  • Restarted the project
  • Cleaned, rebuilt and built the project
  • edited the css file in the wwroot and removed the other file and then did the same in reverse and found the file works when in the css folder in Wwwroot but not in the folder i created

Also I noticed the file is only discovered through href when using the following path ../Styles/form2.css and there are no prompts unlike the href for the wwwroot which is ~/css/form2.css

The way the file is set up the main css file seem to only work when using the href in the parent Shared layout

Solution Explorer

Shared layout Href reference image

Does anyone know why this is happening and how I can fix this or is it a doomed idea to make my own folder for css files?



Solution 1:[1]

Ok as is life in coding the problem i had been working on and trying to resolve for the past 2 days was solved straight after posting a question to stack overflow!

So after some research I found the the answer here relating to Static files within mvc asp .net https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-6.0

From the looking over this my issue was that static files such as HTML, css, Images and Javascript are stored within the project's webroot directory.

However there is a way to get around this by editing the Program.cs file and adding in the following code

app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(  
        Path.Combine(builder.Environment.ContentRootPath, "Styles")),
    RequestPath = "/Styles"
});

See my edit snapshot here (Note: dont forget to add using Microsoft.Extensions.FileProviders;)

Due to some deliberation however I think i will leave the static files to the wwwroot directory and create folders within here but its good to know there is a way to create and use external folders from this default directory if needed for the future!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 GavWasTaken