ASP.NET Core MVC: why the content of wwwroot directory wasn't copied?

Viewed 9080

Visual Studio 2017 (v15.4.4)
ASP.NET Core MVC

I created new ASP.NET Core Web Application from the empty template and filled it. But when I start debugging I see that my CSS-file is not accessible.

The page for the web address was not found http://localhost:51308/css/styles.css
HTTP ERROR 404

Why does it happen and how can I solve this problem?

styles.css:

.field-validation-error {color: #f00;}
.field-validation-valid {display: none;}
.input-validation-error {border: 1px solid #f00; background-color: #fee;}
.validation-summary-errors {font-weight: bold; color: #f00;}
.validation-summary-valid {display: none;}

RsvpForm.cshtml:

@model Razor.Models.GuestResponse
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, Razor

@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>RSVP Form</title>
    <link rel="stylesheet" href="~/css/styles.css"/>
</head>
<body>
    <div>
        This is RSVP form.
        <form asp-action="RsvpForm" method="post">
            <div asp-validation-summary="All"></div>
            <p>
                <label asp-for="Name">Your name:</label>
                <input asp-for="Name" />
            </p>
            <p>
                <label asp-for="Email">Your email:</label>
                <input asp-for="Email" />
            </p>
            <p>
                <label asp-for="Phone">Your phone:</label>
                <input asp-for="Phone" />
            </p>
            <p>
                <label>Will you attend?</label>
                <select asp-for="WillAttend">
                    <option value="">Choose an option</option>
                    <option value="true">Yes I'll be there</option>
                    <option value="false">No, I can't come</option>
                </select>
            </p>
            <button type="submit">Submit RSVP</button>
        </form>
    </div>
</body>
</html>

enter image description here

UPD

I renamed styles.css to site.css and shared this example on GitHub: here is the code source of this example.

2 Answers

You have 2 problems.

  1. You have deactivcated loading static files in your startup.cs Uncomment app.UseStaticFiles(); This is essential in loading javascript, css, html and other static files as images from your wwwroot folder.
  2. You should properly resolve your static file in your razor view, using @Url.Content(): <link href="@Url.Content("~/css/site.css")" rel="stylesheet" /> Just using "~/css/site.css" may work, but will not 100% of the time. I strongly recommend using a shared Layout.cshtml, that takes over loading the css files for you.

This will resolve your issue

  1. You must put all the fixed files like css, JQuery, js and ... in the wwwroot folder to Access it. This is a law in Asp.Net Core.

  2. Also, when you run your project, hit the ctrl + U to display your code and click on the css link. The css commands must be displayed. Apeer was not displayed after a click The files are not loaded correctly.

  3. By default, there is a file named site.css, which you must write all the css commands and your site style there. It linked in the _Layout, you can link your new css file after that.

  4. To link up, just drag the css file from the solution Explorer and drop it in the code box

There is no other case

Related