Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS files not found in ASP.NET 5

I created a Content folder in wwwroot, because I read that client side things like stylesheets and scripts should be added there in the doc, but when I add to a View I have and build the project it does not find the files. What is the correct way to do this ?

<link href="~/content/style.css" rel="stylesheet" />

I am pretty sure the wwwroot is added to project.json correctly.

"webroot": "wwwroot"
like image 980
Dave Demirkhanyan Avatar asked Oct 12 '15 10:10

Dave Demirkhanyan


People also ask

Where is the Wwwroot folder in ASP.NET Core?

The path of the wwwroot folder is accessed using the interfaces IHostingEnvironment (. Net Core 2.0) and IWebHostEnvironment (. Net Core 3.0) in ASP.Net Core.

What does UseDefaultFiles do?

UseDefaultFiles is a URL rewriter that doesn't serve the file. With UseDefaultFiles , requests to a folder in wwwroot search for: default. htm.

What is UseStaticFiles?

The UseStaticFiles is an extension method included in the StaticFiles middleware so that we can easily configure it. Now, open the browser and send http request http://localhost:<port>/default.html which will display default. html as a response as shown below. Serving HTML File.

What are static files in ASP.NET Core?

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.


1 Answers

You need to enable ASP.NET Core static files handling in your Startup:

public void Configure(IApplicationBuilder application)
{
    // Add static files to the request pipeline.
    application.UseStaticFiles();

    // Add MVC to the request pipeline.
    application.UseMvc();
}

Visual Studio 2017 (csproj)

You also need to add this to your csproj unless you are using the Microsoft.AspNetCore.All package:

<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" />

Visual Studio 2015 (xproj + project.json)

You also need to add this to your project.json:

"dependencies": {
    "Microsoft.AspNetCore.StaticFiles": "1.0.0"
    // ...Omitted
}
like image 187
Muhammad Rehan Saeed Avatar answered Oct 06 '22 08:10

Muhammad Rehan Saeed