Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert url path to full or absolute url in ASP.NET MVC?

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?

like image 958
Wai Yan Hein Avatar asked May 19 '16 17:05

Wai Yan Hein


People also ask

How do I get the absolute URL in .NET core?

By specifying the protocol and host, the result is now "https://your-website/Home/Privacy" . However, if you omit the host parameter but keep the protocol parameter, the result will still be an absolute URL, because the host parameter will default to the host used for the current HTTP request.

What is URL rewriting in MVC?

URL rewriting is the concept of changing the currently executing URL and pointing it at some other URL to continue processing the current request or redirecting to an external URL.

What is URL RouteUrl?

RouteUrl(String, RouteValueDictionary, String, String) Generates a fully qualified URL for the specified route values by using the specified route name, protocol to use, and host name.


1 Answers

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.

like image 144
Rion Williams Avatar answered Oct 04 '22 21:10

Rion Williams