【问题标题】:How to get fully qualified path of css file?如何获得css文件的完全限定路径?
【发布时间】:2010-04-17 20:29:24
【问题描述】:

在 ASP.NET MVC 中,如何获取我的 css 文件的完全限定路径 通过指定相对路径。

例如

Url.Content("~/Content/Print.css")

这会返回例如“/Content/Print.css”

随心所欲

http://www.mysite.com/Content/Printcss

了解问题所在?

马尔科姆

【问题讨论】:

  • 您只想在当前请求的上下文中获取它,对吗?

标签: asp.net asp.net-mvc


【解决方案1】:

与 Phil 类似,我会使用 Request 对象。但是,我会查看 Url 属性。 使用 Url,您可以调用 GetLeftPart(UriPartial.Authority) 来获取地址中缺少的部分:

string address = 
    System.Web.HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + 
    Url.Content("~/Content/Print.css");

GetLeftPart 应返回“http://www.mysite.com”,如文档中所示: http://msdn.microsoft.com/en-us/library/system.uri.getleftpart(v=VS.100).aspx

【讨论】:

    【解决方案2】:

    我可能会连接 Request.UserHostName 和您的 CSS 位置:

    String.Format("{0}/Content/Print.css", Request.UserHostName);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-19
      • 2011-04-28
      • 1970-01-01
      • 2015-05-17
      • 1970-01-01
      • 2011-09-29
      • 1970-01-01
      • 2021-06-22
      相关资源
      最近更新 更多