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?
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") %>" />