c# - 无法在虚拟目录中使用 MVC2 在 Site.Master 中正确引用 CSS

标签 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">
<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>

不幸的是,当在“虚拟目录”中的 IIS 6.0 服务器上使用时,CSS 引用无法加载并且页面无法正确呈现。 (对于虚拟目录,我的意思是类似于 http://localhost/MyTestSite,其中“MyTestSite”是在安装了 MVC 应用程序的服务器上的 IIS 管理器中创建的虚拟目录。)

MVC 应用程序运行良好,从中生成的 HTML 加载正常,但服务器似乎无法找到引用的 CSS 和相关图像的位置。我发现这令人费解,因为从 VS2008 运行时它似乎工作得很好。

确实找到了解决我的问题的方法,但我对结果并不完全满意:

<link rel="stylesheet" type="text/css" href=<%= Page.ResolveUrl(@"~/Content/css/layout1_setup.css") %> />
<link rel="stylesheet" type="text/css" href=<%= Page.ResolveUrl(@"~/Content/css/layout1_text.css") %> />

使用 Page.ResolveUrl() 对我来说感觉像是一个 hack,因为它在 VS2008 中编辑时破坏了页面的拆分和/或设计 View 的呈现。 (并且所有 CSS 标记都带有绿色下划线,表示“不存在”。)也就是说,它在“运行”时在 IIS6 和 VS2008 中都呈现得很好。

有没有更好的方法来解决这个问题?

编辑: 我的问题听起来像这里描述的问题:http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx -- 但我已经修复了 default.aspx.cs 文件,如下所示。

   public void Page_Load(object sender, System.EventArgs e)
   {
        string originalPath = Request.Path;
        HttpContext.Current.RewritePath(Request.ApplicationPath, false);
 // Setting "false" on the above line is supposed to fix my issue, but it doesn't.
        IHttpHandler httpHandler = new MvcHttpHandler();
        httpHandler.ProcessRequest(HttpContext.Current);
        HttpContext.Current.RewritePath(originalPath, false);
   }

最佳答案

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

已编辑:

经过一番思考后,我意识到当使用 VS 2008 时,在“ASP.Net Development Server”下运行网站时,您可能正在使用 Debug模式,而当您部署到 IIS 时,您可能已经在 Release模式下发布了代码。

如果是这种情况,您可以尝试以下操作:

<% #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 代码完成工具将在虚拟目录中运行您的网站(作为发布版本)。

关于c# - 无法在虚拟目录中使用 MVC2 在 Site.Master 中正确引用 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3859193/

相关文章:

c# - LINQ 代码中的异步 - 说明?

c# - 无法将类型 System.Collections.ObjectModel.ObservableCollection<> 隐式转换为 System.Collections.Generic.List<>

c# - Azure 表存储 ExecuteBatchAsync 未运行异步

html - 如何永久对齐堆叠元素的 div

javascript - toastr:页面加载 n 秒后显示

python - 如何更改具有特定 id 的 QMenuBar 的子 css 样式表?

c# - 在 Web 应用程序中使用 C# Windows 应用程序项目

asp.net-mvc - DisplayNameFor 标签更改文本

c# - 如何重定向所有错误,包括 404

asp.net-mvc - 当参数名称需要不同时,如何重用远程验证方法?