You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core MVC项目CSS样式仅首页生效问题求助

问题根源

你这问题是样式路径没写对——那些没加~/前缀的相对路径,在首页(根路由)能正常解析,但跳转到子路由页面(比如/Home/About)时,浏览器会把css/style.css当成/Home/css/style.css找,而你的文件实际在wwwroot/css下面,自然找不到,所有样式就失效了。

直接改法

把所有本地样式的路径前面加上~/,让ASP.NET Core帮你生成正确的根相对路径。修正后的<head>代码如下:

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - FIDEL ENTERPRISE</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
    <link rel="stylesheet" href="~/Final_WebApplication.styles.css" asp-append-version="true" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <meta name="author" content="" />
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.min.css" />
    <!-- 这里加了~/前缀,修正路径 -->
    <link rel="stylesheet" type="text/css" href="~/css/bootstrap.css" />
    <link href="https://fonts.googleapis.com/css?family=Poppins:400,600,700&display=swap" rel="stylesheet">
    <!-- 这两个也加了~/ -->
    <link href="~/css/style.css" rel="stylesheet" />
    <link href="~/css/responsive.css" rel="stylesheet" />
</head>
额外提醒
  • 你同时引入了bootstrap.min.css和bootstrap.css,属于重复加载,留一个就行,优先选min版(压缩过,加载更快)。
  • 检查wwwroot/css下的文件名和路径拼写完全一致,大小写不能出错(尤其是部署到Linux服务器时,系统对大小写敏感)。
  • 不用给每个页面单独加样式链接,只要_Layout里的路径正确,所有继承该布局的页面都会自动加载样式,没必要重复操作。

内容的提问来源于stack exchange,提问作者Abiy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 23:06:17