How to handle relative paths in ASP.NET MVC?

I have a master page which references a style in the following manner:

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

All my pages inherit from this master page. And this works well when the URL is http://www.domain.com/home/details/5, however the URL is http://www.domain.com/home/create, then, of course, Style.css cannot be found because `../../Content/Style.css' resolves to a directory one higher where there is nothing there.

How is this typically handled?

Answers


Use Url.Content("~/Content/Style.css") to resolve the path safely.

"~" means the host. e.g.

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

Need Your Help

Listing Blogger Posts via API v3 with PHP Google_Client, Google_Service_Blogger

php blogger google-api-php-client

I am using the Google PHP client library for accessing Google APIs (See reference)

How to Reset scrollbar to top of window

c# winforms

I am working on a Winform app using .net 4.5.