简体   繁体   English

为什么我的样式表在MVC上使用HayWire?

[英]Why is my Style Sheet Going HayWire on MVC?

I am working on familiarizing myself with the MVC on ASP.NET. 我正在努力使自己熟悉ASP.NET上的MVC。 I have on my global.asax 我在global.asax上

routes.MapRoute("Dept", "Dept/Invoice/{Category}/{Name}", 
new {controller = "Dept", action = "Invoice", Category = "", Name = ""});

The Controller.cs code gets the data and put it in a table which is fine. Controller.cs代码获取数据并将其放在表格中。 My html starts... 我的html开始...

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<BMICPolicy.Dept>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<link rel="Stylesheet" type="text/css" href="../../Content/Test.css" />
<html xmlns="http://www.w3.org/1999/xhtml">

Style Sheet - 样式表-

body
{
    font-family: Arial, Verdana, Book Antiqua, Sans-Serif;
    font-size: 12px;
    width: 100%;
    height: 100%;
    min-height: 100%;
    text-align: center;
     text-align: -moz-center;
    padding: 0px;
    margin: 0px;
    position: relative;
}
table 
{
  border: solid 1px #000000;
  border-collapse: collapse;  
}

table td 
{
  padding: 5px;   
  border: solid 1px #000000;  
}

table th
{
  padding: 6px 5px;  
  background-color: #729FCB; 
  border: solid 1px #000000;   
  text-align: center;
}

thead
{
    /*font-size: medium;*/
    font: large, bold;
    text-align: center;
}

I am not sure why the Style sheet only works when I enter up to "Category" and my style sheet is completely disregarded when I type the name in the address bar (Body font style, table, td, th, thead, etc...). 我不确定为什么只有在输入“ Category”时,样式表才起作用,而当我在地址栏中键入名称(正文字体样式,表格,td,th,thead等)时,样式表却被完全忽略了。 )。 Am I missing something? 我想念什么吗?

../../Content/Test.css is a relative URI, and means "look for a folder called Content in the parent folder of the parent folder of the current location, and in that folder find a file called Test.css . ../../Content/Test.css是相对URI,表示“在当前位置的父文件夹的父文件夹中查找名为Content的文件夹,然后在该文件夹中找到一个名为Test.css的文件。

When you only enter a category, you're looking for a sibling of the Dept folder called Content . 仅输入类别时,您正在寻找名为ContentDept文件夹的同级对象。 When you enter a name as well, you're looking for a sibling of the Invoice folder. 当您输入名称时,您正在寻找Invoice文件夹的同级文件。

Perhaps you want to use /Content/Test.css as the path to your stylesheet instead. 也许您想使用/Content/Test.css作为样式表的路径。

That's because your current path is treated as a nested folder hierarchy and the relative path simply does not work (your css is being search for in Dept/Invoice/Content/Test.css or something like this, which is obviously incorrect). 那是因为您当前的路径被视为嵌套的文件夹层次结构,并且相对路径根本不起作用(您的CSS正在Dept/Invoice/Content/Test.css或类似的搜索中,这显然是不正确的)。 This is a common situation with style and script files, especially when they are placed in the master page. 这是样式和脚本文件的常见情况,尤其是当它们放置在母版页中时。

The right thing to do in this situation is to use Url.Content as explained in this thread . 在这种情况下,正确的做法是使用此线程中介绍的Url.Content Imho this works better than an absolute path. 恕我直言,这比绝对路径更好。

change this line: 更改此行:

<link rel="Stylesheet" type="text/css" href="../../Content /Test.css" /> <link rel="Stylesheet" type="text/css" href="../../Content /Test.css” />

to this : 对此:

<link rel="Stylesheet" type="text/css" href="<%=ResolveUrl("~/Content/Test.css ") %>" /> <link rel="Stylesheet" type="text/css" href="<%=ResolveUrl("~/Content/Test.css “)%>” />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM