What is equivalent to .ResolveUrl page in ASP.NET MVC?

What is the equivalent of the .ResolveUrl page in ASP.NET MVC available in the controller?

+63
asp.net-mvc
Mar 16 '10 at 8:30
source share
7 answers

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" /> 
+95
Mar 16 '10 at 8:40
source share

This should do what you are looking for ...

 System.Web.VirtualPathUtility.ToAbsolute ("~ /")
+38
Mar 16 '10 at 14:27
source share

Here are some ways to solve the path that this root application operator uses ( ~ )

To call any method with embedded code on an asp.net page, this method must either be shown as an instance variable for the current object, or available as a static / general method.

A typical MVC page gives us access to quite a few of them as properties through WebViewPage . Have you ever wondered when you type @ViewData , do you get a magical connection to ViewData? This is because you are in a property set on the MVC page where you are located.

Therefore, to call these methods, we do not necessarily refer to the type that they represent, but to the instance property that provides them.

We can call the above instance methods similar to this (respectively):

 href="@Url.Content("~/index.html")" href="@Server.MapPath("~/index.html")" href="@Href("~/index.html")" 

We can do this to call a generic method that does not need an instance:

 href="@VirtualPathUtility.ToAbsolute("~/index.html")" 

AFAIK, the MVC page does not automatically instantiate anything from the System.Web.UI namespace from which ResolveUrl is inherited. If for some reason you really wanted to use this particular method, you could just update the control and use the methods that it provides, but I would recommend against it .

 @Code Dim newControl As New System.Web.UI.Control Dim resolvedUrl = newControl.ResolveUrl("~/index.html") End Code href="@resolvedUrl" 

To keep things said, I would recommend using @Url.Content as it is best suited for MVC paradigms

+6
Dec 01 '14 at
source share

UrlHelper.Content() does the same thing as Control.ResolveUrl().

For further links: http://stephenwalther.com/archive/2009/02/18/asp-net-mvc-tip-47-ndash-using-resolveurl-in-an-html.aspx

+3
Aug 17 2018-12-18T00:
source share

You no longer need to do this in Razor v2.0 / ASP.NET MVC 4.

Just use the β€œ~” on the razor page and he will allow it for you.

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

Source

0
Nov 02 '16 at 21:52
source share
 Server.MapPath() //returna full path 

or

 url.content() 
-3
Mar 16 '10 at 8:31
source share

try using Server.MapPath ().

-6
Mar 16 '10 at 8:33
source share



All Articles