Getting Absolute URLs for ASP.NET MVC Action

This is probably a fictitious question, but I cannot find a clear indication. I have a POCO class in an MVC3 web application whose sole purpose is to manage the backup of some files on the server. It usually backs up and returns the file name to the controller, which sends an email with the URL to download it. This works fine, but I cannot create an absolute URL to submit. No matter what function I use, I always get a relative URL, for example /Backup/TheFile.zip, and not for example http://www.somesite.com/Backup/TheFile.zip . I tried:

VirtualPathUtility.ToAbsolute("~/Backup/SomeFile.zip"); HttpRuntime.AppDomainAppVirtualPath + "/Backup/SomeFile.zip"; Url.Content("~/Backup/SomeFile.zip"); 

but they all return something like /Backup/SomeFile.zip. Any idea?

+56
c # asp.net-mvc asp.net-mvc-3
Sep 13 '11 at 17:54
source share
6 answers

You can do it as follows:

 var urlBuilder = new System.UriBuilder(Request.Url.AbsoluteUri) { Path = Url.Action("Action", "Controller"), Query = null, }; Uri uri = urlBuilder.Uri; string url = urlBuilder.ToString(); // or urlBuilder.Uri.ToString() 

Instead of Url.Action() in this example, you can also use Url.Content() or any routing method, or just simply go through the path.

But if the URL goes to Controller Action , there is a more compact way:

 var contactUsUriString = Url.Action("Contact-Us", "About", routeValues: null /* specify if needed */, protocol: Request.Url.Scheme /* This is the trick */); 

The trick here is that after specifying the protocol / scheme, when you call any routing method, you get an absolute URL. I recommend this when possible , but you also have a more general path in the first example, if you need one.

I talked about this in detail here:
http://gurustop.net/blog/2012/03/23/writing-absolute-urls-to-other-actions-in-asp-net-mvc/

Extracted from Meligys AngularJS and Web Dev Goodies Newsletter

+90
Mar 23 '12 at 3:18
source share

Inside the controller:

 var path = VirtualPathUtility.ToAbsolute(pathFromPoco); var url = new Uri(Request.Url, path).AbsoluteUri 
+34
May 09 '12 at 23:50
source share

This works for me:

 using System; using System.Web; using System.Web.Mvc; public static class UrlExtensions { public static string Content(this UrlHelper urlHelper, string contentPath, bool toAbsolute = false) { var path = urlHelper.Content(contentPath); var url = new Uri(HttpContext.Current.Request.Url, path); return toAbsolute ? url.AbsoluteUri : path; } } 

Usage in cshtml:

 @Url.Content("~/Scripts/flot/jquery.flot.menuBar.js", true) 
+17
Jun 01 '14 at 8:42 on
source share

The built-in helpers in MVC 4 create absolute URLs if the host or protocol parameters are not empty. See this answer here for an example of an extension method for use in views.

+4
Feb 19 '13 at 5:36
source share

I wrote a helper class for this, for MVC 5 ... It is quite flexible and especially useful if you need this functionality when you are not inside the controller. You must be able to transfer it directly to the project and leave.

As Meligi noted, the key should include the protocol. Here I have it hardcoded as http, so if you want to use SSL, which might turn out to be a little more flexible.

 public class AbsoluteUrlHelper { /// <summary> /// Creates an absolute "fully qualified" url from an action, and assumes the current controller. /// </summary> /// <returns></returns> public static string GetAbsoluteUrl(string action, object routeValues = null) { var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext); var values = urlHelper.RequestContext.RouteData.Values; var controller = values["controller"].ToString(); return GetAbsoluteUrl(action, controller, urlHelper, routeValues); } /// <summary> /// Creates an absolute "fully qualified" url from an action and controller. /// </summary> public static string GetAbsoluteUrl(string action, string controller, object routeValues = null) { var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext); return GetAbsoluteUrl(action, controller, urlHelper, routeValues); } /// <summary> /// Creates an absolute "fully qualified" url from an action and controller. /// </summary> public static string GetAbsoluteUrl(string action, string controller, UrlHelper urlHelper, object routeValues = null) { var uri = urlHelper.Action(action, controller, routeValues, "http"); return uri; } } 
0
Jul 08 '15 at 19:43
source share

You have several options:

  • Store the value of HttpContext.Request.Url in a static or member variable and use this to pass the full path.
  • Save the application domain in the application settings in the web.config file.
  • Hard code value.
-four
Sep 13 '11 at 18:29
source share



All Articles