【问题标题】:How to handle relative paths in ASP.NET MVC?如何处理 ASP.NET MVC 中的相对路径?
【发布时间】:2011-01-29 23:12:43
【问题描述】:

我有一个以下列方式引用样式的母版页:

<link rel="stylesheet" type="text/css" href="../../Content/Style.css" /> 

我的所有页面都继承自该母版页。这在 URL 为 http://www.domain.com/home/details/5 时效果很好,但是 URL 为 http://www.domain.com/home/create,那么当然找不到 Style.css,因为 `../../Content/Style.css' 解析为一个目录更高的地方,那里什么都没有。

这通常是如何处理的?

【问题讨论】:

    标签: asp.net-mvc visual-studio-2008 .net-3.5 relative-path


    【解决方案1】:

    使用 Url.Content("~/Content/Style.css") 安全地解析路径。

    “~”表示主机。 例如

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

    【讨论】:

    猜你喜欢
    • 2012-09-23
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多