Is there a built-in way of getting the full URL of an action?
I am looking for something like GetFullUrl("Action", "Controller")
that would return something like http://www.fred.com/Controller/Action
.
The reason I am looking for this is to avoid hardcoding URLs in automated emails that are generated so that the URLs can always be generated relative to the current location of the site.
There is an overload of Url.Action that takes your desired protocol (e.g. http, https) as an argument - if you specify this, you get a fully qualified URL.
Here's an example that uses the protocol of the current request in an action method:
var fullUrl = this.Url.Action("Edit", "Posts", new { id = 5 }, this.Request.Url.Scheme);
HtmlHelper (@Html) also has an overload of the ActionLink method that you can use in razor to create an anchor element, but it also requires the hostName and fragment parameters. So I'd just opt to use @Url.Action again:
<span>
Copy
<a href='@Url.Action("About", "Home", null, Request.Url.Scheme)'>this link</a>
and post it anywhere on the internet!
</span>
As Paddy mentioned: if you use an overload of UrlHelper.Action()
that explicitly specifies the protocol to use, the generated URL will be absolute and fully qualified instead of being relative.
I wrote a blog post called How to build absolute action URLs using the UrlHelper class in which I suggest to write a custom extension method for the sake of readability:
/// <summary>
/// Generates a fully qualified URL to an action method by using
/// the specified action name, controller name and route values.
/// </summary>
/// <param name="url">The URL helper.</param>
/// <param name="actionName">The name of the action method.</param>
/// <param name="controllerName">The name of the controller.</param>
/// <param name="routeValues">The route values.</param>
/// <returns>The absolute URL.</returns>
public static string AbsoluteAction(this UrlHelper url,
string actionName, string controllerName, object routeValues = null)
{
string scheme = url.RequestContext.HttpContext.Request.Url.Scheme;
return url.Action(actionName, controllerName, routeValues, scheme);
}
You can then simply use it like that in your view:
@Url.AbsoluteAction("Action", "Controller")
This what you need to do.
@Url.Action(action,controller, null, Request.Url.Scheme)
This question is specific to ASP .NET however I am sure some of you will benefit of system agnostic javascript which is beneficial in many situations.
UPDATE: The way to get url formed outside of the page itself is well described in answers above.
Or you could do a oneliner like following:
new UrlHelper(actionExecutingContext.RequestContext).Action(
"SessionTimeout", "Home",
new {area = string.Empty},
actionExecutingContext.Request.Url!= null?
actionExecutingContext.Request.Url.Scheme : "http"
);
from filter or:
new UrlHelper(this.Request.RequestContext).Action(
"Details",
"Journey",
new { area = productType },
this.Request.Url!= null? this.Request.Url.Scheme : "http"
);
However quite often one needs to get the url of current page, for those cases using Html.Action
and putting he name and controller of page you are in to me feels awkward. My preference in such cases is to use JavaScript instead. This is especially good in systems that are half re-written MVT half web-forms half vb-script half God knows what - and to get URL of current page one needs to use different method every time.
One way is to use JavaScript to get URL is window.location.href
another - document.URL
I was having an issue with this, my server was running behind a load balancer. The load balancer was terminating the SSL/TLS connection. It then passed the request to the web servers using http.
Using the Url.Action() method with Request.Url.Schema, it kept creating a http url, in my case to create a link in an automated email (which my PenTester didn't like).
I may have cheated a little, but it is exactly what I needed to force a https url:
<a href="@Url.Action("Action", "Controller", new { id = Model.Id }, "https")">Click Here</a>
I actually use a web.config AppSetting so I can use http when debugging locally, but all test and prod environments use transformation to set the https value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With