【问题标题】:ASP.NET MVC 4 Site.css won't loadASP.NET MVC 4 Site.css 不会加载
【发布时间】:2013-06-14 10:56:12
【问题描述】:

在 Visual Studio 2012 中,我创建了一个新的 ASP.NET MVC 4 项目,但我没有编辑任何内容。当我在 Google Chrome 中使用 IIS Express 调试这个项目时,网站一直在加载。如果我按 F12 并查看“网络”选项卡,Site.css 只会保持“待处理”状态并且永远不会加载。 Internet Explorer 9 显示相同的行为。其余文件(HTML en Javascript)似乎可以毫无问题地加载。网站文件在我的本地系统上,我已授予“每个人”对文件夹的完全权限。

我尝试了多种方法,例如创建新项目并将文件夹移到我的用户配置文件之外,但结果保持不变。如果我注释掉 Site.css 文件的链接,我的网站会立即加载而不会出现任何问题。

IIS Express 的日志甚至没有显示对 Site.css 文件的 GET 请求。

#Software: Microsoft Internet Information Services 8.0
#Version: 1.0
#Date: 2013-06-18 08:59:13
#Fields: date time s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) cs(Referer) sc-status sc-substatus sc-win32-status time-taken
2013-06-18 08:59:13 ::1 GET / - 62073 - ::1 Mozilla/5.0+(Windows+NT+6.1;+WOW64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/27.0.1453.94+Safari/537.36 - 200 0 0 15154
2013-06-18 08:59:13 ::1 GET /Scripts/modernizr-2.6.2.js - 62073 - ::1 Mozilla/5.0+(Windows+NT+6.1;+WOW64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/27.0.1453.94+Safari/537.36 http://localhost:62073/ 200 0 0 42
2013-06-18 08:59:13 ::1 GET /Scripts/jquery-1.8.2.js - 62073 - ::1 Mozilla/5.0+(Windows+NT+6.1;+WOW64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/27.0.1453.94+Safari/537.36 http://localhost:62073/ 200 0 0 124

根据要求:这是 IIS Express 提供的 HTML 页面的源代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Home Page - My ASP.NET MVC Application</title>
    <link href="/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <meta name="viewport" content="width=device-width" />
    <link href="/Content/Site.css" rel="stylesheet"/>

    <script src="/Scripts/modernizr-2.6.2.js"></script>

</head>
<body>
    <header>
        <div class="content-wrapper">
            <div class="float-left">
                <p class="site-title"><a href="/">your logo here</a></p>
            </div>
            <div class="float-right">
                <section id="login">
                        <ul>
    <li><a href="/Account/Register" id="registerLink">Register</a></li>
    <li><a href="/Account/Login" id="loginLink">Log in</a></li>
</ul>

                </section>
                <nav>
                    <ul id="menu">
                        <li><a href="/">Home</a></li>
                        <li><a href="/Home/About">About</a></li>
                        <li><a href="/Home/Contact">Contact</a></li>
                    </ul>
                </nav>
            </div>
        </div>
    </header>
    <div id="body">

<section class="featured">
    <div class="content-wrapper">
        <hgroup class="title">
            <h1>Home Page.</h1>
            <h2>Modify this template to jump-start your ASP.NET MVC application.</h2>
        </hgroup>
        <p>
            To learn more about ASP.NET MVC visit
            <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
            The page features <mark>videos, tutorials, and samples</mark> to help you get the most from ASP.NET MVC.
            If you have any questions about ASP.NET MVC visit
            <a href="http://forums.asp.net/1146.aspx/1?MVC" title="ASP.NET MVC Forum">our forums</a>.
        </p>
    </div>
</section>

        <section class="content-wrapper main-content clear-fix">

<h3>We suggest the following:</h3>
<ol class="round">
<li class="one">
    <h5>Getting Started</h5>
    ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that
    enables a clean separation of concerns and that gives you full control over markup
    for enjoyable, agile development. ASP.NET MVC includes many features that enable
    fast, TDD-friendly development for creating sophisticated applications that use
    the latest web standards.
    <a href="http://go.microsoft.com/fwlink/?LinkId=245151">Learn more…</a>
</li>

<li class="two">
    <h5>Add NuGet packages and jump-start your coding</h5>
    NuGet makes it easy to install and update free libraries and tools.
    <a href="http://go.microsoft.com/fwlink/?LinkId=245153">Learn more…</a>
</li>

<li class="three">
    <h5>Find Web Hosting</h5>
    You can easily find a web hosting company that offers the right mix of features
    and price for your applications.
    <a href="http://go.microsoft.com/fwlink/?LinkId=245157">Learn more…</a>
</li>
</ol>

        </section>
    </div>
    <footer>
        <div class="content-wrapper">
            <div class="float-left">
                <p>&copy; 2013 - My ASP.NET MVC Application</p>
            </div>
        </div>
    </footer>

    <script src="/Scripts/jquery-1.8.2.js"></script>


</body>
</html>

【问题讨论】:

  • 显示呈现 CSS 引用的代码和呈现的 HTML 本身。可能是你的捆绑搞砸了。
  • 你能从渲染页面发布页面源代码吗?您是否尝试在 _Layout.cshtml 中对样式表的引用进行硬编码?
  • 我将页面的来源添加到问题中。据我所知,它并没有什么特别之处。我尝试对参考进行硬编码,但这不起作用。我什至尝试通过将链接粘贴到 URL 栏中来通过浏览器打开 css,但这也不起作用。 (我也尝试使用其他 css 文件,如 \Content\themes\base\jquery-ui.css )
  • 请记住,在部署大多数路径时都会被搞砸。我建议使用虚拟目录或解决方案@JochemTheSchoolKid

标签: css asp.net-mvc-4 visual-studio-2012 iis-express


【解决方案1】:

你在用铬吗?尝试按 ctrl+shift+del 并删除缓存。

【讨论】:

    【解决方案2】:

    问题似乎与缓存有关。我发生了类似的事情,我附加了一个 ?__v=1234 并且它起作用了。对于这个内容,我现在提供如下内容:

    String.Format("{0}?_v={1}", Url.Content("~/Content/foo.css"), DateTime.Now.Ticks)
    

    【讨论】:

    • 在目标浏览器的 URL 上使用临时查询字符串有助于强制重新下载缓存的 CSS 和 Javascript 文件,尤其是在某些版本的 IE 和某些移动浏览器(如 iPad Safari)上。我发现这在进行频繁更新时很有帮助。
    【解决方案3】:

    尝试:

    <link href="~/Content/Site.css" rel="stylesheet" type="text/css">
    

    【讨论】:

    • 试过了,结果还是一样。 Site.css 一直处于“待处理”状态。
    【解决方案4】:

    我不确定这是否有什么不同,我只是一个初学者,但我使用这个:

    <link href="@Url.Content("~/Content/style.css")" rel="stylesheet" type="text/css" />
    

    【讨论】:

    • 添加类型也不起作用。但我认为问题出在 IIS Express 上,因为它甚至没有提供在我的问题日志中看到的请求的 CSS 文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    • 2015-01-14
    相关资源
    最近更新 更多