C# 无法在站点中正确引用CSS。在虚拟目录中使用MVC2的Master
目前,我的MVC应用程序有一个C# 无法在站点中正确引用CSS。在虚拟目录中使用MVC2的Master,c#,css,asp.net-mvc,virtual-directory,master-pages,C#,Css,Asp.net Mvc,Virtual Directory,Master Pages,目前,我的MVC应用程序有一个Site.Master页面,当直接从VS2008运行时,该页面效果非常好。看起来是这样的: <%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&g
Site.Master
页面,当直接从VS2008运行时,该页面效果非常好。看起来是这样的:
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="../../Content/css/layout1_setup.css" />
<link rel="stylesheet" type="text/css" href="../../Content/css/layout1_text.css" />
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
</head>
编辑:
经过一些思考,我重新认识到,当使用VS2008时,在“ASP.Net开发服务器”下运行网站时,您可能正在使用调试模式,当您部署到IIS时,您可能已经在发布模式下发布了代码
如果是这种情况,您可以尝试以下方法:
<% #if DEBUG %>
<link rel="stylesheet" type="text/css" href="../../Content/css/layout1_setup.css" />
<link rel="stylesheet" type="text/css" href="../../Content/css/layout1_text.css" />
<% #else %>
<link rel="stylesheet" type="text/css" href="<%= Url.Content("~/Content/css/layout1_setup.css") %>" />
<link rel="stylesheet" type="text/css" href="<%= Url.Content("~/Content/css/layout1_text.css") %>" />
<% #endif %>
现在,当您在Visual Studio 2008中运行时,您的CSS代码完成工具将与在虚拟目录中运行您的网站(作为发布版本)一样工作。刚刚测试了以下内容,以确保它能够解决这两个问题(正确启用设计视图和解决)。希望对你有用
<link rel="stylesheet" type="text/css" href="~/Content/css/layout1_setup.css" runat="server" />
<link rel="stylesheet" type="text/css" href="~/Content/css/layout1_text.css" runat="server" />
John Hartsock已启用,但他尝试执行的预处理器命令在设计模式下无法按预期工作(我认为它实际上尝试同时执行这两个命令)。相反,您可以尝试检查.NET
站点
属性,该属性可用于测试是否在设计模式下运行(在版本配置中,站点
属性并不总是填充,因此您还必须检查它是否为空)
另外,Visual Studio design viewer不知道域和虚拟应用程序路径,因此在设计器中,您可以使用/
指向应用程序根目录
<% if (Site != null && Site.DesignMode) { %>
<link href="/Content/css/layout1_setup.css" rel="stylesheet"/>
<% } else { %>
<link href="<%= Url.Content("~/Content/css/layout1_setup.css") %>" rel="stylesheet"/>
<% } %>
恐怕没有优雅的方法可以做到这一点。您可以执行以下可怕的黑客行为来欺骗设计师:
<% if (false) { %>
<!-- That's just to cheat the designer, it will never render at runtime -->
<link rel="stylesheet" type="text/css" href="../../Content/css/layout1_setup.css" />
<link rel="stylesheet" type="text/css" href="../../Content/css/layout1_text.css" />
<% } %>
<link rel="stylesheet" type="text/css" href="<%= Url.Content("~/Content/css/layout1_setup.css") %>" />
<link rel="stylesheet" type="text/css" href="<%= Url.Content("~/Content/css/layout1_text.css") %>" />
就我个人而言,我从未使用过设计器,也从未在我的代码中做过类似的事情,但是如果你真的需要这个设计视图,那么它可能是一个解决方案
我的意思是你在一个ASP.NETMVC项目中工作,你应该处理html,为什么要关心设计视图?看看为了让设计视图正常工作而应该支付的价格,它太贵了。在您喜爱的浏览器中按CTRL+F5键查看您的工作结果比在“代码”和“设计”视图之间切换更快。这可能是显而易见的,或者我错过了一些东西,但这看起来像是路径问题。您正在使用相对路径(
。/../
)。我相信,当您在Visual Studio中运行某些东西时,应用程序是根路径(即项目主目录中的default.aspx
将是localhost:port/default.aspx
)。如果任何页面中的相对路径进入过多目录(即。/
过多次),它将被忽略并从网站的根目录获取(在本例中为localhost:port/
)。例如,如果文件夹结构如下所示:
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="../../Content/css/layout1_setup.css" />
<link rel="stylesheet" type="text/css" href="../../Content/css/layout1_text.css" />
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
</head>
- 认可
- 样式(文件夹)
- 内容(文件夹)
- 其他文件(文件夹)
- myfile.aspx
- default.aspx
。/content/
从myfile.aspx
访问content
文件夹,或者,即使您不应该这样做,也可以使用。/../content/
仅当approt与域根目录相同(即localhost:port/content/
和domain.com/content/
是同一个文件夹)时才有效.但是,如果您现在将这些文件放在web服务器上的另一个(虚拟)文件夹(即
domain.com/virtual
==newapproot)中。/../content
fromdomain.com/virtual/otherfiles/myfile.aspx
将引用domain.com/content/
,这是不正确的
我希望这会有所帮助。与我现有的解决方案相比,没有什么更好或更糟的地方。在IIS6和VS2008中运行时看起来很好,但在通过设计和/或分割视图进行编辑时,无法找到CSS。不过,谢谢你的建议。@Pretzel.。我想出了一个你可能喜欢的解决方案。嗯。。。这看起来是答案。让我试试。@John:我一整天都在追踪一只虫子。还没有机会测试它。我现在就试试。看起来Awe的解决方案对我来说是有效的,但我认为你让他走上了正确的轨道,所以我选了你一个。只是在VS中试了一下。不起作用。-奇怪的是,如果我使用“/Content/css/layout1_setup.css”(删除~)并将其粘贴到旁边,然后按Enter键,web浏览器就会下载css表。这令人困惑。所以波浪号(~)表示从根开始。但这是否意味着在-或-这是否意味着(因为这个网站是基于这个虚拟目录“MySite”。)好问题。。。它可能要求您的虚拟目录是一个“应用程序”,它考虑虚拟目录的根来确定虚拟路径。你可以试试。。。右键单击IIS中的虚拟目录并说“转换为应用程序”。当您在服务器上运行上述代码时。。。它输出的html是什么(什么路径)?我正在运行IIS 6.0--我在IIS管理器中没有看到“转换为应用程序”。当我运行您的代码时,它会准确地发出您编写的源代码。不过我试过一个实验。我在指定“~/Content/css/layout\u text.css”的地方添加了一个-A HREF-link,鼠标悬停在链接上方显示“谢谢,伙计”。这是对我有用的答案。非常感谢。:-)我一直生活在设计视图中被破坏的视图中,但有些人觉得它应该在这两种视图中都起作用。似乎是一个简单的疏忽。我高估了你的票数,因为我有点喜欢你的黑,即使它很难看。:-)
<% if (Site != null && Site.DesignMode) { %>
<link href="/Content/css/layout1_setup.css" rel="stylesheet"/>
<% } else { %>
<link href="<%= Url.Content("~/Content/css/layout1_setup.css") %>" rel="stylesheet"/>
<% } %>
<% if (false) { %>
<!-- That's just to cheat the designer, it will never render at runtime -->
<link rel="stylesheet" type="text/css" href="../../Content/css/layout1_setup.css" />
<link rel="stylesheet" type="text/css" href="../../Content/css/layout1_text.css" />
<% } %>
<link rel="stylesheet" type="text/css" href="<%= Url.Content("~/Content/css/layout1_setup.css") %>" />
<link rel="stylesheet" type="text/css" href="<%= Url.Content("~/Content/css/layout1_text.css") %>" />