What is the equivalent to Page.ResolveUrl in ASP.NET MVC?

asp.net Mvc

asp.net Mvc Problem Overview


What is the equivalent to Page.ResolveUrl in ASP.NET MVC available in the Controller?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

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" />

Solution 2 - asp.net Mvc

This should do what you're looking for...

System.Web.VirtualPathUtility.ToAbsolute("~/")

Solution 3 - asp.net Mvc

Here are a whole bunch of ways to resolve a path that uses that application root operator (~)

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

Solution 4 - asp.net Mvc

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

Solution 5 - asp.net Mvc

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" />

Source

Solution 6 - asp.net Mvc

Server.MapPath() //returna full path

or

url.content()

Solution 7 - asp.net Mvc

try using Server.MapPath().

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMark RedmanView Question on Stackoverflow
Solution 1 - asp.net MvcAtanas KorchevView Answer on Stackoverflow
Solution 2 - asp.net MvcCarloView Answer on Stackoverflow
Solution 3 - asp.net MvcKyleMitView Answer on Stackoverflow
Solution 4 - asp.net MvcRahul RanjanView Answer on Stackoverflow
Solution 5 - asp.net MvcCarter MedlinView Answer on Stackoverflow
Solution 6 - asp.net MvcPaul CreaseyView Answer on Stackoverflow
Solution 7 - asp.net MvcAshish GuptaView Answer on Stackoverflow