0

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

when I use Url.Content, it converts "~/css/themes/default/app.css" to "/css/themes/default/app.css". What I need though is something like "../../css/themes/default/app.css"
those files are outside your root ?
I guess I'm not sure. The whole routing thing in MVC regularly confuses me. I will say that I have my css directory in MVC.csproj/resources and my cshtml files are in MVC.csproj/Views
Use the conventions MVC provides. Keep your css/images inside the Content folder and use the HTML Helper methods
is there anyway to salvage it if I'm already a long ways down my current path? Or is the only way to restructure my folders?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.