【问题标题】:master page does not load css file母版页不加载 css 文件
【发布时间】:2015-08-13 22:26:42
【问题描述】:

所以我在母版页中引用了这样的 css 文件:

<link rel="stylesheet" type="text/css" href="~/default.css" runat="server" />

它实际上在设计视图中显示在 Visual Studio 中,但由于某种原因未显示在 Web 浏览器中。这是为什么?顺便说一句,css 文件和母版页以及图像文件夹都在根目录中。 css 文件:

*
{
padding: 0em;
margin: 0em;
}

body
{
background:url('images/bg1.jpg');
padding: 30px 0px 35px 0px;
}

body,input
{
font-size: 10pt;
font-family: "georgia", "times new roman", serif;
color: #333333;
}

p
{
line-height: 1.5em;
margin-bottom: 1.0em;
text-align: justify;
}

a
{
color: #B96D00;
text-decoration: underline;
}

a:hover
{
text-decoration: none;
}


h3 span
{
font-weight: normal;
}

h3,h4
{
display: inline;
font-weight: bold;
background-repeat: no-repeat;
background-position: right;
}

h3
{
font-size: 1.7em;
padding-right: 34px;
background-image: url('images/db1.gif');
}

h4
{
font-size: 1.2em;
padding-right: 28px;
background-image: url('images/db2.gif');
}

.contentarea
{
padding-top: 1.3em;
}

img
{
border: solid 1px #6F5230;
}

img.left
{
position: relative;
float: left;
margin: 0em 1.8em 1.4em 0em;
}

img.right
{
position: relative;
float: right;
margin: 0em 0em 1.8em 1.8em;
}

.divider1
{
position: relative;
background: #fff url('images/border2.gif') repeat-x;
height: 14px;
margin: 2.0em 0em 1.5em 0em;
clear: both;
}

.divider2
{
position: relative;
height: 1px;
border-bottom: solid 1px #eaeaea;
margin: 2.0em 0em 2.0em 0em;
}

.post .details
{
position: relative;
top: -1.5em;
font-size: 0.8em;
color: #787878;
}

.post ul.controls
{
clear: both;
}

.post ul.controls li
{
display: inline;
font-size: 0.8em;
}

.post ul.controls li a
{
background-repeat: no-repeat;
background-position: left;
padding: 0em 1.0em 0em 20px;
}

.post ul.controls li a.printerfriendly
{
background-image: url('images/icon-printerfriendly.gif');
}

.post ul.controls li a.comments
{
background-image: url('images/icon-comments.gif');
}

.post ul.controls li a.more
{
background-image: url('images/icon-more.gif');
}

.box
{
position: relative;
background: #FDFCF6 url('images/boxbg.gif') repeat-x;
left: -1.5em;
top: -1.5em;
padding: 1.5em;
border-bottom: solid 1px #E1D2BD;
margin-bottom: 1.0em;
}

ul.linklist
{
list-style: none;
}

ul.linklist li
{
line-height: 2.0em;
}

#upbg
{
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 275px;
background: #fff url('images/bg2.jpg') repeat-x;
z-index: 1;
}

#outer
{
z-index: 2;
position: relative;
width: 82%;
border: solid 7px #fff;
background-color: #fff;
margin: 0 auto;
}

#header
{
position: relative;
width: 100%;
height: 8.0em;
background: #2B2B2B url('images/topbg.gif') repeat-x;
margin-bottom: 2px;
}

#headercontent
{
position: absolute;
bottom: 0em;
padding: 0em 2.0em 1.3em 2.0em;
}

#headercontent h1
{
font-weight: normal;
color: #fff;
font-size: 2.5em;
}

#headercontent h1 sup
{
color: #777;
}

#headercontent h2
{
font-size: 1.0em;
font-weight: normal;
color: #aaa;
}

#search
{
position: absolute;
top: 5.5em;
right: 2.0em;
padding-right: 0.0em;
}

#search input.text
{
margin-right: 0.5em;
vertical-align: middle;
border-top: solid 1px #000000;
border-right: 0px;
border-bottom: solid 1px #777777;
border-left: 0px;
padding: 0.15em;
width: 10.0em;
}

#search input.submit
{
background: #939B00 url('images/buttonbg.gif') repeat-x;
border: solid 1px #5F6800;
font-weight: bold;
padding: 0.25em;
font-size: 0.8em;
color: #F2F3DE;
vertical-align: middle;
}

#headerpic
{
position: relative;
height: 200px;
background: #fff url('images/hdrpic.jpg') no-repeat top left;
margin-bottom: 2px;
}

#menu
{
position: relative;
background: #7F8400 url('images/menubg.gif') repeat-x top left;
height: 3.5em;
padding: 0em 1.0em 0em 1.0em;
margin-bottom: 2px;
}

#menu ul
{
position: absolute;
top: 1.1em;
}

#menu ul li
{
position: relative;
display: inline;
}

#menu ul li a
{
padding: 0.5em 1.0em 0.9em 1.0em;
color: #fff;
text-decoration: none;
}

#menu ul li a:hover
{
text-decoration: underline;
}

#menu ul li a.active
{
background: #7F8400 url('images/menuactive.gif') repeat-x top left;
}

#menubottom
{
background: #fff url('images/border1.gif') repeat-x;
height: 14px;
margin-bottom: 1.5em;
}

#content
{
padding: 0em 2.0em 0em 2.0em;
}

#primarycontainer
{
float: left;
margin-right: -18.0em;
width: 100%;
}

#primarycontent
{
margin: 1.5em 22.0em 0em 0em;
}

#secondarycontent
{
margin-top: 1.5em;
float: right;
width: 18.0em;
}

#footer
{
position: relative;
height: 2.0em;
clear: both;
padding-top: 5.0em;
background: #fff url('images/border2.gif') repeat-x 0em 2.5em;
font-size: 0.8em;
}

#footer .left
{
position: absolute;
left: 2.0em;
bottom: 1.2em;
}

#footer .right
{
position: absolute;
right: 2.0em;
bottom: 1.2em;
}

母版页:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="Project3.Site1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <title>Zahra Moradi</title>
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <link rel="stylesheet" type="text/css" href="~/default.css" runat="server"  />
</head>
<body>
    <form id="form1" runat="server">
    <div id="upbg">
    </div>
    <div id="outer">
        <div id="header">
            <div id="headercontent">
                <h1>
                    E-Library</h1>
            </div>
        </div>
        <form method="post" action="">
        <div id="search">
            <input type="text" class="text" maxlength="64" name="keywords" />
            <input type="submit" class="submit" value="Search" />
        </div>
        </form>
        <div id="headerpic">
        </div>
        <div id="menu">
            <!-- HINT: Set the class of any menu link below to "active" to make it appear active -->
            <ul>
                <li><a href="Home.aspx">Home</a></li>
                <li><a href="Library.aspx">Library</a></li>
                <li><a href="About.aspx">About</a></li>
                <li><a href="Contact.aspx">Contact</a></li>
            </ul>
        </div>
        <div id="menubottom">
        </div>
        <div id="content">
            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
            </asp:ContentPlaceHolder>
        </div>
        <div id="footer">
            <div class="left">
                &copy; 2015 Zahra Moradi. All rights reserved.</div>
            <div class="right">
                Design by <a href="http://www.nodethirtythree.com/">NodeThirtyThree Design</a></div>
        </div>
    </div>
    </form>
</body>
</html>

我已经尝试过针对类似问题发布的其他解决方案,但对我不起作用。比如添加 runat="server" 等等...

【问题讨论】:

  • @Tyr 我实际上尝试了该问题的解决方案,但对我不起作用
  • 看看你的网络标签,它是否给你的 CSS 文件一个 404 错误?
  • 尝试像http://host/pathtocss/file.css一样直接调用css文件来查看它是否存在,或者使用浏览器的网络选项卡来查看它是否已加载。

标签: javascript html css asp.net master-pages


【解决方案1】:

与@Tyr 相同的建议。尝试将 CSS 文件加载为 Web 链接而不是本地文件。例如:

<link rel="stylesheet" type="text/css" href="https://example.com/website/css/default.css"/>

应该可以。如果没有,请使用浏览器上的开发人员工具(最好使用 Google Chrome)。在 Chrome 中,右键单击页面上的任意位置,然后单击“检查元素”。右侧栏上显示的 CSS 文件是否有任何错误?如果有,它们是什么?

【讨论】:

  • 但我没有使用任何网络主机。我正在使用视觉工作室。我如何使用网络链接。
  • Herehere 可能会有所帮助。
  • 我找到了哪里出错了。在 web.config 中,我拒绝访问未经授权的用户,并且由于 css 文件与 home.aspx 位于同一目录中,因此未加载,因此我将 &lt;location path="default.css"&gt; &lt;system.web&gt; &lt;authorization&gt; &lt;allow users="*"/&gt; &lt;/authorization&gt; &lt;/system.web&gt; &lt;/location&gt; 添加到 web.config 文件中。现在我还有另一个问题,图像无法加载
  • 对图像执行与 CSS 文件相同的操作吗?
猜你喜欢
  • 2016-01-03
  • 2015-06-19
  • 2011-02-12
  • 2010-12-11
  • 2018-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-03
相关资源
最近更新 更多