Getting full URL of action in ASP.NET MVC [duplicate]

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="https://stackoverflow.com/questions/2005367/@Url.Action("About", "Home", null, Request.Url.Scheme)">this link</a> 
  and post it anywhere on the internet!
</span>

Leave a Comment