gpt4 book ai didi

css - ASP.NET 核心 MVC : why the content of wwwroot directory wasn't copied?

转载 作者:行者123 更新时间:2023-11-28 11:02:22 25 4
gpt4 key购买 nike

Visual Studio 2017 (v15.4.4)
ASP.NET 核心 MVC

我从空模板创建了新的 ASP.NET Core Web 应用程序并填充了它。但是当我开始调试时,我发现我的 CSS 文件不可访问。

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

为什么会发生这种情况,我该如何解决这个问题?

样式.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

我将 styles.css 重命名为 site.css 并在 GitHub 上分享了这个示例:here是本例的代码源。

最佳答案

你有两个问题。

  1. 您已停用在 startup.cs 中加载静态文件取消注释 app.UseStaticFiles();这对于从您的 wwwroot 文件夹中加载 javascript、css、html 和其他静态文件作为图像至关重要。
  2. 您应该使用@Url.Content() 正确解析razor View 中的静态文件。 : <link href="@Url.Content("~/css/site.css")" rel="stylesheet" />仅使用“~/css/site.css”可能有效,但不是 100% 的时间。我强烈建议使用共享的 Layout.cshtml,它会为您加载 css 文件。

这将解决您的问题

关于css - ASP.NET 核心 MVC : why the content of wwwroot directory wasn't copied?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47375681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com