How do I find the absolute url of an action in ASP.NET MVC?

asp.net Mvc

asp.net Mvc Problem Overview


I need to do something like this:

<script type="text/javascript">
    token_url = "http://example.com/your_token_url";
</script>

I'm using the Beta version of MVC, but I can't figure out how to get the absolute url of an action. I'd like to do something like this:

<%= Url.AbsoluteAction("Action","Controller")) %>

Is there a helper or Page method for this?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

Click here for more information, but esentially there is no need for extension methods. It's already baked in, just not in a very intuitive way.

Url.Action("Action", null, null, Request.Url.Scheme);

Solution 2 - asp.net Mvc

Extend the UrlHelper

namespace System.Web.Mvc
{
    public static class HtmlExtensions
    {
        public static string AbsoluteAction(this UrlHelper url, string action, string controller)
        {
            Uri requestUrl = url.RequestContext.HttpContext.Request.Url;

            string absoluteAction = string.Format(
                "{0}://{1}{2}",
                requestUrl.Scheme,
                requestUrl.Authority,
                url.Action(action, controller));

            return absoluteAction;
        }
    }
}

Then call it like this

<%= Url.AbsoluteAction("Dashboard", "Account")%>

EDIT - RESHARPER ANNOTATIONS

The most upvoted comment on the accepted answer is This answer is the better one, this way Resharper can still validate that the Action and Controller exists. So here is an example how you could get the same behaviour.

using JetBrains.Annotations

namespace System.Web.Mvc
{
    public static class HtmlExtensions
    {
        public static string AbsoluteAction(
            this UrlHelper url,
            [AspMvcAction]
            string action,
            [AspMvcController]
            string controller)
        {
            Uri requestUrl = url.RequestContext.HttpContext.Request.Url;

            string absoluteAction = string.Format(
                "{0}://{1}{2}",
                requestUrl.Scheme,
                requestUrl.Authority,
                url.Action(action, controller));

            return absoluteAction;
        }
    }
}

Supporting info:

Solution 3 - asp.net Mvc

<%= Url.Action("About", "Home", null, Request.Url.Scheme) %>
<%= Url.RouteUrl("Default", new { Action = "About" }, Request.Url.Scheme) %>

 

Solution 4 - asp.net Mvc

Using @Charlino 's answer as a guide, I came up with this.

The ASP.NET MVC documentation for UrlHelper shows that Url.Action will return a fully-qualified Url if a hostname and protocol are passed in. I created these helpers to force the hostname and protocol to be provided. The multiple overloads mirror the overloads for Url.Action:

using System.Web.Routing;

namespace System.Web.Mvc {
	public static class HtmlExtensions {

		public static string AbsoluteAction(this UrlHelper url, string actionName) {
			Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
			return url.Action(actionName, null, (RouteValueDictionary)null, 
                              requestUrl.Scheme, null);
		}

		public static string AbsoluteAction(this UrlHelper url, string actionName, 
                                            object routeValues) {
			Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
			return url.Action(actionName, null, new RouteValueDictionary(routeValues), 
                              requestUrl.Scheme, null);
		}

		public static string AbsoluteAction(this UrlHelper url, string actionName, 
                                            RouteValueDictionary routeValues) {
			Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
			return url.Action(actionName, null, routeValues, requestUrl.Scheme, null);
		}

		public static string AbsoluteAction(this UrlHelper url, string actionName, 
                                            string controllerName) {
			Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
			return url.Action(actionName, controllerName, (RouteValueDictionary)null, 
                              requestUrl.Scheme, null);
		}

		public static string AbsoluteAction(this UrlHelper url, string actionName, 
                                            string controllerName, 
                                            object routeValues) {
			Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
			return url.Action(actionName, controllerName, 
                              new RouteValueDictionary(routeValues), requestUrl.Scheme, 
                              null);
		}

		public static string AbsoluteAction(this UrlHelper url, string actionName, 
                                            string controllerName, 
                                            RouteValueDictionary routeValues) {
			Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
			return url.Action(actionName, controllerName, routeValues, requestUrl.Scheme, 
                              null);
		}

		public static string AbsoluteAction(this UrlHelper url, string actionName, 
                                            string controllerName, object routeValues, 
                                            string protocol) {
			Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
			return url.Action(actionName, controllerName, 
                              new RouteValueDictionary(routeValues), protocol, null);
		}

	}
}

Solution 5 - asp.net Mvc

I'm not sure if there is a built in way to do it, but you could roll your own HtmlHelper method.

Something like the following

namespace System.Web.Mvc
{
    public static class HtmlExtensions
    {
        public static string AbsoluteAction(this HtmlHelper html, string actionUrl)
        {
            Uri requestUrl = html.ViewContext.HttpContext.Request.Url;

            string absoluteAction = string.Format("{0}://{1}{2}",
                                                  requestUrl.Scheme,
                                                  requestUrl.Authority,
                                                  actionUrl);
        
            return absoluteAction;
        }
    }
}

Then call it like this

<%= Html.AbsoluteAction(Url.Action("Dashboard", "Account"))%> »

HTHs, Charles

Solution 6 - asp.net Mvc

Complete answer with arguments would be :

var url = Url.Action("ActionName", "ControllerName", new { id = "arg_value" }, Request.Url.Scheme);

and that will produce an absolute url

Solution 7 - asp.net Mvc

Same result but a little cleaner (no string concatenation/formatting):

public static Uri GetBaseUrl(this UrlHelper url)
{
    Uri contextUri = new Uri(url.RequestContext.HttpContext.Request.Url, url.RequestContext.HttpContext.Request.RawUrl);
    UriBuilder realmUri = new UriBuilder(contextUri) { Path = url.RequestContext.HttpContext.Request.ApplicationPath, Query = null, Fragment = null };
    return realmUri.Uri;
}

public static string ActionAbsolute(this UrlHelper url, string actionName, string controllerName)
{
    return new Uri(GetBaseUrl(url), url.Action(actionName, controllerName)).AbsoluteUri;
}

Solution 8 - asp.net Mvc

Maybe this (?):

<%= 
  Request.Url.GetLeftPart(UriPartial.Authority) + 
  Url.Action("Action1", "Controller2", new {param1="bla", param2="blabla" })
%>

Solution 9 - asp.net Mvc

> env: dotnet core version 1.0.4

Url.Action("Join",null, null,Context.Request.IsHttps?"https":"http");

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
QuestionMike ComstockView Question on Stackoverflow
Solution 1 - asp.net MvcAdam BoddingtonView Answer on Stackoverflow
Solution 2 - asp.net MvcCharlinoView Answer on Stackoverflow
Solution 3 - asp.net MvcRyan SampsonView Answer on Stackoverflow
Solution 4 - asp.net MvcRaleigh BucknerView Answer on Stackoverflow
Solution 5 - asp.net MvcCharlinoView Answer on Stackoverflow
Solution 6 - asp.net MvcDashrathView Answer on Stackoverflow
Solution 7 - asp.net MvcveggerbyView Answer on Stackoverflow
Solution 8 - asp.net MvctytusseView Answer on Stackoverflow
Solution 9 - asp.net MvcguhyeonView Answer on Stackoverflow