JavaScript Developer
JavaScript Developer

Reputation: 4008

Using Virtual Paths in ASP.NET MVC 3

I have a layout page which is shared across all of the views in my app. That view references the .css in my app. For the sake of reference, I currently have the following at the top of my _layout.cshtml file:

<link rel="stylesheet" href="/css/themes/default/app.css" />

I am planning on taking this app and loading into a phone app via PhoneGap. Because of that, I cannot use /css/themes/default.app.css. Instead, I need to get it converted to a string that is relative to the path of the file. For instance, I need to dynamically generate a value that looks something like the following:

<link rel="stylesheet" href="../../../css/themes/default/app.css" />

The number of "../" will be determined based on how deep it is. I figured there would be a utility or something built into the ASP.NET MVC 3 framework to do this. However, I can't find anything. I have a LOT of files and I don't want to have to manually update all of the url patterns. Is there a utility that will automatically handle what I'm trying to acomplish? If so, how?

Thank you

Upvotes: 0

Views: 552

Answers (1)

Shyju
Shyju

Reputation: 218892

Use the UrlHelper.Content HTML Helper method .

This method Converts a virtual (relative) path to an application absolute path.

<link href="@Url.Content("~/css/themes/default/app.css")" 
                                       rel="stylesheet" type="text/css" />     

You may drop your css folder under the Content directory in the root of your MVC project

Upvotes: 1

Related Questions