How to turn a relative URL into a full URL?

This is probably explained more easily with an example. I am trying to find a way to convert a relative url for example. "/Foo.aspx" or "~ / Foo.aspx" to the full URL, for example. http: //localhost/Foo.aspx . Thus, when I deploy a test or step where the domain under which the site operates is different, I get http: //test/Foo.aspx and http: //stage/Foo.aspx .

Any ideas?

+59
Sep 24 '08 at 9:41
source share
12 answers

Play with it (change here )

public string ConvertRelativeUrlToAbsoluteUrl(string relativeUrl) { return string.Format("http{0}://{1}{2}", (Request.IsSecureConnection) ? "s" : "", Request.Url.Host, Page.ResolveUrl(relativeUrl) ); } 
+57
Sep 24 '08 at 9:46
source share

This one was beaten to death, but I thought I would publish my own decision, which, in my opinion, is cleaner than many other answers.

 public static string AbsoluteAction(this UrlHelper url, string actionName, string controllerName, object routeValues) { return url.Action(actionName, controllerName, routeValues, url.RequestContext.HttpContext.Request.Url.Scheme); } public static string AbsoluteContent(this UrlHelper url, string path) { Uri uri = new Uri(path, UriKind.RelativeOrAbsolute); //If the URI is not already absolute, rebuild it based on the current request. if (!uri.IsAbsoluteUri) { Uri requestUrl = url.RequestContext.HttpContext.Request.Url; UriBuilder builder = new UriBuilder(requestUrl.Scheme, requestUrl.Host, requestUrl.Port); builder.Path = VirtualPathUtility.ToAbsolute(path); uri = builder.Uri; } return uri.ToString(); } 
+37
Jan 26 2018-12-12T00:
source share

You just need to create a new URI using page.request.url and then get AbsoluteUri from this:

 New System.Uri(Page.Request.Url, "Foo.aspx").AbsoluteUri 
+30
Sep 15 '11 at 8:01
source share

This is my helper function for this.

 public string GetFullUrl(string relativeUrl) { string root = Request.Url.GetLeftPart(UriPartial.Authority); return root + Page.ResolveUrl("~/" + relativeUrl) ; } 
+5
May 7 '10 at 12:42
source share

I thought I shared my approach to this in ASP.NET MVC using the Uri class and some extension magic.

 public static class UrlHelperExtensions { public static string AbsolutePath(this UrlHelper urlHelper, string relativePath) { return new Uri(urlHelper.RequestContext.HttpContext.Request.Url, relativePath).ToString(); } } 

Then you can infer the absolute path using:

 // gives absolute path, eg https://example.com/customers Url.AbsolutePath(Url.Action("Index", "Customers")); 

It looks a little ugly having nested method calls, so I prefer to extend UrlHelper with common action methods so that I can:

 // gives absolute path, eg https://example.com/customers Url.AbsoluteAction("Index", "Customers"); 

or

 Url.AbsoluteAction("Details", "Customers", new{id = 123}); 

The full extension class is as follows:

 public static class UrlHelperExtensions { public static string AbsolutePath(this UrlHelper urlHelper, string relativePath) { return new Uri(urlHelper.RequestContext.HttpContext.Request.Url, relativePath).ToString(); } public static string AbsoluteAction(this UrlHelper urlHelper, string actionName, string controllerName) { return AbsolutePath(urlHelper, urlHelper.Action(actionName, controllerName)); } public static string AbsoluteAction(this UrlHelper urlHelper, string actionName, string controllerName, object routeValues) { return AbsolutePath(urlHelper, urlHelper.Action(actionName, controllerName, routeValues)); } } 
+3
Jan 21 '13 at 14:06
source share

Use the .NET Uri class to combine your relative path and hostname.
http://msdn.microsoft.com/en-us/library/system.uri.aspx

+2
Sep 24 '08 at 9:47
source share

This is a helper function that I created for the conversion.

 //"~/SomeFolder/SomePage.aspx" public static string GetFullURL(string relativePath) { string sRelative=Page.ResolveUrl(relativePath); string sAbsolute=Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery,sRelative); return sAbsolute; } 
+1
Nov 10 '10 at 16:40
source share

Simply:

 url = new Uri(baseUri, url); 
+1
Sep 17
source share

In ASP.NET MVC, you can use HtmlHelper or UrlHelper overloads that accept protocol or host parameters. When any of these parameters is not empty, helpers generate an absolute URL. This is the extension method I use:

 public static MvcHtmlString ActionLinkAbsolute<TViewModel>( this HtmlHelper<TViewModel> html, string linkText, string actionName, string controllerName, object routeValues = null, object htmlAttributes = null) { var request = html.ViewContext.HttpContext.Request; var url = new UriBuilder(request.Url); return html.ActionLink(linkText, actionName, controllerName, url.Scheme, url.Host, null, routeValues, htmlAttributes); } 

And use it from a Razor view, for example:

  @Html.ActionLinkAbsolute("Click here", "Action", "Controller", new { id = Model.Id }) 
+1
Feb 19 '13 at 5:33
source share

An ancient question, but I thought I would answer it, since many answers are incomplete.

 public static string ResolveFullUrl(this System.Web.UI.Page page, string relativeUrl) { if (string.IsNullOrEmpty(relativeUrl)) return relativeUrl; if (relativeUrl.StartsWith("/")) relativeUrl = relativeUrl.Insert(0, "~"); if (!relativeUrl.StartsWith("~/")) relativeUrl = relativeUrl.Insert(0, "~/"); return $"{page.Request.Url.Scheme}{Uri.SchemeDelimiter}{page.Request.Url.Authority}{VirtualPathUtility.ToAbsolute(relativeUrl)}"; } 

This works like a page extension, just like ResolveUrl and ResolveClientUrl for web forms. Feel free to convert it to the HttpResponse extension if you want or need to use it in an environment without web forms. It handles both http and https correctly on standard and non-standard ports, and if there is a username / password component. It also does not use hard-coded strings (namely: //).

0
May 9 '16 at 17:34
source share

Here is the approach. It does not matter if the string is relative or absolute, but you must provide baseUri to use it.

  /// <summary> /// This function turns arbitrary strings containing a /// URI into an appropriate absolute URI. /// </summary> /// <param name="input">A relative or absolute URI (as a string)</param> /// <param name="baseUri">The base URI to use if the input parameter is relative.</param> /// <returns>An absolute URI</returns> public static Uri MakeFullUri(string input, Uri baseUri) { var tmp = new Uri(input, UriKind.RelativeOrAbsolute); //if it absolute, return that if (tmp.IsAbsoluteUri) { return tmp; } // build relative on top of the base one instead return new Uri(baseUri, tmp); } 

In an ASP.NET context, you can do this:

 Uri baseUri = new Uri("http://yahoo.com/folder"); Uri newUri = MakeFullUri("/some/path?abcd=123", baseUri); // //newUri will contain http://yahoo.com/some/path?abcd=123 // Uri newUri2 = MakeFullUri("some/path?abcd=123", baseUri); // //newUri2 will contain http://yahoo.com/folder/some/path?abcd=123 // Uri newUri3 = MakeFullUri("http://google.com", baseUri); // //newUri3 will contain http://google.com, and baseUri is not used at all. // 
0
Jun 17 '16 at 2:30 p.m.
source share

Changed from another answer for working with localhost and other ports ... im using for. Email. You can call from any part of the application, not only on the page or in usercontrol, I put this in Global, because you do not need to pass HttpContext.Current.Request as a parameter

  /// <summary> /// Return full URL from virtual relative path like ~/dir/subir/file.html /// usefull in ex. external links /// </summary> /// <param name="rootVirtualPath"></param> /// <returns></returns> public static string GetAbsoluteFullURLFromRootVirtualPath(string rootVirtualPath) { return string.Format("http{0}://{1}{2}{3}", (HttpContext.Current.Request.IsSecureConnection) ? "s" : "" , HttpContext.Current.Request.Url.Host , (HttpContext.Current.Request.Url.IsDefaultPort) ? "" : ":" + HttpContext.Current.Request.Url.Port , VirtualPathUtility.ToAbsolute(rootVirtualPath) ); } 
0
Apr 22 '17 at 4:31 on
source share



All Articles