5

I am developing Web Application using ASP.NET MVC in C#. But I am having a problem with retrieving full or absolute url. In ASP.NET MVC we get url like this. Url.Content("~/path/to/page"). It will return "path/to/page". But what I want to do is I have a string like this - "~/controller/action".

Let's consider my website domain is www.example.com. If I use Url.Content("~/controller/action"), it will just return "controller/action". I want to get "www.example.com/controller/action". How can I get it?

halfer
  • 19,824
  • 17
  • 99
  • 186
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
  • Have you tried [this](http://stackoverflow.com/questions/434604/how-do-i-find-the-absolute-url-of-an-action-in-asp-net-mvc): `Url.Action("action", "controller", null, Request.Url.Scheme)`? – Jasen May 19 '16 at 17:25

2 Answers2

14

If you can use the Controller / Action Names...

You should use the Url.Action() method for this.

Typically, Url.Action() will return something similar to what you presently expect when provided with just the Controller and Action names :

// This would yield "Controller/Action"
Url.Action("Action","Controller"); 

However, when you pass in the protocol parameter (i.e. http, https etc.) then the method will actually return a complete, absolute URL. For the sake of convienence, you can use the Request.Url.Scheme property to access the appropriate protocol as seen below :

// This would yield "http://your-site.com/Controller/Action"
Url.Action("Action", "Controller", null, Request.Url.Scheme);

You can see an example of this in action here.

If you only have a relative URL string...

If you only have access to something like a relative URL (i.e. ~/controller/action), then you may want to create a function that will extend the current functionality of the Url.Content() method to support serving absolute URLs :

public static class UrlExtensions
{
    public static string AbsoluteContent(this UrlHelper urlHelper, string contentPath)
    {
        // Build a URI for the requested path
        var url = new Uri(HttpContext.Current.Request.Url, urlHelper.Content(contentPath));
        // Return the absolute UrI
        return url.AbsoluteUri;
    }
}

If defined properly, this would allow you to simply replace your Url.Content() calls with Url.AbsoluteContent() as seen below :

Url.AbsoluteContent("~/Controller/Action")

You can see an example of this approach here.

Rion Williams
  • 74,820
  • 37
  • 200
  • 327
  • I am sorry. This could not solve my problem. This is good answer but. What I want is I want to convert string like "~/Controller/Action" to full url. – Wai Yan Hein May 19 '16 at 19:00
  • I've updated the answer now that I realize that you wanted to actually use the relative URL. Let me know if you run into any issues with it. – Rion Williams May 19 '16 at 19:13
0

The following will render a full url, including http or https:

var url = new UrlHelper(System.Web.HttpContext.Current.Request.RequestContext);
var fullUrl = url.Action("YourAction", "YourController", new { id = something }, protocol: System.Web.HttpContext.Current.Request.Url.Scheme);

Output

https://www.yourdomain.com/YourController/YourAction?id=something

Alfred Wallace
  • 1,741
  • 1
  • 14
  • 32