ASP.NET MVC - 在母版页中引用样式表

发布于 2024-08-18 17:29:21 字数 397 浏览 3 评论 0原文

我有一个位于 /Views/Shared 中的母版页。母版页引用 /Content 文件夹中的样式表。

如果我使用 "../../Content/style.css" 引用样式表,一切都会正常工作。但是,我的Web应用程序不在我们生产环境的根文件夹中,因此相对路径不起作用。

我尝试过“<%=ResolveUrl("~/content/style.css") %>"这在生产场景中确实有效,但随后 Visual Studio 中的设计器抱怨我的类是错误的(并且我无法在设计选项卡中预览带有 CSS 的页面)。

有没有一种解决方案可以在这两种情况下都起作用?我在 WebForms 中通过编写重置链接标记的服务器端代码来完成此操作。我可以在这里这样做,但我想避免这样做。

I have a master page that is in /Views/Shared. The master page references a stylesheet in the /Content folder.

Everything works fine if I reference the stylesheet using "../../Content/style.css". However, my web application is not in the root folder in our production environment, so the relative path doesn't work.

I have tried "<%=ResolveUrl("~/content/style.css") %>" which does work in the production scenario, but then the designer in Visual Studio complains about my classes being wrong (and I cannot preview the page with CSS in the design tab).

Is there a solution that makes this work in both situations? I accomplished this in WebForms by writing server-side code that reset the link tag. I could do that here, but I would like to avoid it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遗失的美好 2024-08-25 17:29:21

尝试这种技术 - 以两种方式包含您的样式表。包含一个具有固定路径引用的引用,Visual Studio 将使用该引用来提供设计时支持,但将其包含在服务器端注释中,因此在运行时期间实际上不会包含它。第二个引用是运行时使用的“真实”引用,并且使用 Url.Content() 无论您的应用程序是否是子目录,它都可以工作。

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

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

Try this technique - include your stylesheet both ways. Include one with a fixed path reference that Visual Studio will use for design-time support, but enclose it in server-side comments so it's not actually included during run-time. The second reference is the "real" reference used at run-time, and with Url.Content() it'll work whether your app is a sub directory or not.

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

<link href="<%=Url.Content("~/Content/Site.css") %>" rel="stylesheet" 
      type="text/css" />
半世晨晓 2024-08-25 17:29:21

最佳实践是扩展 URL 帮助程序< /a>.这使您可以轻松地从您的视图中调用它,并且如果您的结构或文件发生变化,您不需要进行大量的查找/替换。

public static string Image(this UrlHelper helper, string fileName)  
{  
    return helper.Content("~/Content/Images/" + fileName));  
}  

public static string Stylesheet(this UrlHelper helper, string fileName)  
{  
    return helper.Content("~/Content/Stylesheets/" + fileName);  
}  

public static string Script(this UrlHelper helper, string fileName)  
{  
    return helper.Content("~/Content/Scripts/" + fileName);  
}

   <link href="<%= UrlHelper.Stylesheet("Main.css")%>" rel="stylesheet" 
         type="text/css" />  

It is best practice to Extend the URL Helper. This allows you to easily call it from your view, and if your structure or files change, you don't need to do a massive find/replace.

public static string Image(this UrlHelper helper, string fileName)  
{  
    return helper.Content("~/Content/Images/" + fileName));  
}  

public static string Stylesheet(this UrlHelper helper, string fileName)  
{  
    return helper.Content("~/Content/Stylesheets/" + fileName);  
}  

public static string Script(this UrlHelper helper, string fileName)  
{  
    return helper.Content("~/Content/Scripts/" + fileName);  
}

   <link href="<%= UrlHelper.Stylesheet("Main.css")%>" rel="stylesheet" 
         type="text/css" />  
绻影浮沉 2024-08-25 17:29:21

在 Views 文件夹中,然后进入 Shared 文件夹有助于了解 MVC 中如何引用 CSS 文件。

In the Views folder and then going into the Shared folder helps understand how the CSS file is referenced in the MVC.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文