What is the equivalent to Page.ResolveUrl in ASP.NET MVC available in the Controller?
9 Answers
It is Url.Content:
ASPX:
<link rel="stylesheet" href="<%= Url.Content("~/Content/style.css") %>" type="text/css" />
Razor:
<link rel="stylesheet" href="@Url.Content("~/Content/style.css")" type="text/css" />

- 30,562
- 8
- 59
- 93
-
4And don't overlook that "~/" bit like I did, as it's important – Slider345 Oct 11 '11 at 20:21
This should do what you're looking for...
System.Web.VirtualPathUtility.ToAbsolute("~/")
-
2Although I mostly use Url.Content, this method can be useful as it is static and doesn't require a request context, like Url.Content. – R. Schreurs Jul 05 '13 at 12:09
-
This can be used on a .ascx that is consumed by both web forms and MVC. – kevinpo May 15 '17 at 14:00
Here are a whole bunch of ways to resolve a path that uses that application root operator (~
)
UrlHelper.Content
HttpServerUtility.MapPath
WebPageExecutingBase.Href
VirtualPathUtility.ToAbsolute
Control.ResolveUrl
To call any method with inline code on an asp.net page, the method either needs to be exposed as an instance variable on the current object, or available as a static/shared method.
A typical MVC page gives us access to quite a few of these as properties via the WebViewPage
. Ever wonder when you type @ViewData
, you get magically wired up to the ViewData? That's because you have hit a property exposed by the MVC page you're on.
So to call these methods, we don't necessarily refer to the type they represent, but the instance property that exposes them.
We can call the above instance methods like this (respectively):
href="@Url.Content("~/index.html")"
href="@Server.MapPath("~/index.html")"
href="@Href("~/index.html")"
We can do this to call a shared method that doesn't need an instance:
href="@VirtualPathUtility.ToAbsolute("~/index.html")"
AFAIK, an MVC page doesn't automatically create an instance of anything from the System.Web.UI namespace, from which ResolveUrl
inherits. If, for some reason, you really wanted to use that particular method, you could just new up a control and use the methods it exposes, but I would highly recommend against it.
@Code
Dim newControl As New System.Web.UI.Control
Dim resolvedUrl = newControl.ResolveUrl("~/index.html")
End Code
href="@resolvedUrl"
That all said, I would recommend using @Url.Content
as it fits best with MVC paradigms

- 30,350
- 66
- 462
- 664
UrlHelper.Content()
does the same thing as Control.ResolveUrl().
For Further References: http://stephenwalther.com/archive/2009/02/18/asp-net-mvc-tip-47-ndash-using-resolveurl-in-an-html.aspx

- 1,855
- 18
- 29

- 1,028
- 1
- 10
- 27
You don't need to do this anymore in Razor v2.0/ASP.NET MVC 4.
Just use the "~" in a razor page and it will resolve it for you.
<link rel="stylesheet" href="~/Content/style.css" type="text/css" />

- 11,857
- 5
- 62
- 68
In my case, I find @Href not being enough in the way it deals with query strings in a URL. I prefer to wrap it inside the Raw method:
<script>
var isKendoWindow = false;
var myTimeOut;
clearTimeout(myTimeOut);
var sessionTimeout = (@Session.Timeout * 60000) - 5;
function doRedirect() {
if (!isKendoWindow)
window.location.href = '@Html.Raw(Href("~/Logon.aspx?brandid=" + SessionController.LandingBrandId + "&errCode=5055"))';
}
myTimeOut = setTimeout('doRedirect()', sessionTimeout);
</script>
Or you can create your own version like this:
public static IHtmlString ResolveUrl(this HtmlHelper htmlHelper, string url)
{
var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
return htmlHelper.Raw(urlHelper.Content(url));
}

- 5
- 2
Another way to solve this issue:
Resolve the url in a code block at the top of the page or in code behind.
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
Layout = "~/Pages/Shared/_IndexLayout.cshtml";
String img1 = Url.Content("~/img/people11.jpg");
}
Then use the variable in the html.
<div class="col-12 col-lg-8" style="background-image: url('@img1');"> </div>
-
P.S. This method also works around the problem of the page not parsing correctly with multiple nested quotes. https://social.msdn.microsoft.com/Forums/en-US/35c09604-84eb-4539-8361-883376cfba85/problem-with-nested-single-and-double-quotes-in-embedded-code-blocks-in-the-markup?forum=aspwebforms – Michael W Ritchie Dec 14 '22 at 16:25
Server.MapPath() //returna full path
or
url.content()

- 28,321
- 10
- 54
- 90
-
Doesnt Server.MapPath() return the physical disk path rather than expanding a full URL, like Page.ResolveUrl("~") ? – Mark Redman Mar 16 '10 at 08:38
-
1yeah your right it does, you could hack around it but `url.content` is available instead! – Paul Creasey Mar 16 '10 at 08:43