Malcolm
Malcolm

Reputation: 12864

How to get fully qualified path of css file?

In ASP.NET MVC how do I get the fully qualified path to my css file by specifying the relative path.

Eg

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

This returns eg "/Content/Print.css"

Where as I want

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

Understand the issue?

Malcolm

Upvotes: 2

Views: 1061

Answers (2)

BJ Safdie
BJ Safdie

Reputation: 3419

Similar to Phil, I would use the Request object. However, I would look at the Url property. With the Url, you can call GetLeftPart(UriPartial.Authority) to get the missing part of your address:

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

The GetLeftPart should return "http://www.mysite.com" as shown in the doc: http://msdn.microsoft.com/en-us/library/system.uri.getleftpart(v=VS.100).aspx

Upvotes: 6

Phil.Wheeler
Phil.Wheeler

Reputation: 16848

I'd probably concatenate Request.UserHostName and your CSS location:

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

Upvotes: 0

Related Questions