Dave Demirkhanyan
Dave Demirkhanyan

Reputation: 570

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"

Upvotes: 8

Views: 5916

Answers (2)

Muhammad Rehan Saeed
Muhammad Rehan Saeed

Reputation: 38437

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
}

Upvotes: 13

Amit
Amit

Reputation: 46323

The tilde ("~") symbol translates to base url, but that happens in server code, through server controls or other server side url manipulation functions.

For client side URL, remove the tilde.

Upvotes: 0

Related Questions