Get absolute url in asp.net

When I run my site with visual studio, its root URL is: http://localhost:4657

I have lines containing paths like ~/Login.aspx and I need to concatenate them so the return is http://localhost:4657/Login.aspx .

I see many ways to do this, but how to?

0
Apr 28 2018-11-21T00:
source share
3 answers

Try Page.ResolveUrl .

 string url = Page.ResolveUrl("~/Login.aspx"); 

If you need the full URL, tell me to send it by email or something else, check out this blog post .

+5
Apr 28 '11 at 19:32
source share

What about Request.Url? Url.Authority provides the host name or Socket (IPAddress: PortNo), and segments provide other parts of the URL. Just omit the LAST segment as it contains the current page name. Therefore, i-1 *.

  string myurl = "http://"+Request.Url.Authority; for(int i=0;i<Request.Url.Segments.Length-1;i++) { myurl = myurl + Request.Url.Segments[i]; } myurl = myurl + "login.aspx"; Response.Redirect(myurl); 
0
Mar 01 '12 at 13:50
source share

This is what I use

 var baseUri = new Uri(HttpContext.Current.Request.Url, "/"); 
0
Oct 22 '15 at 15:31
source share



All Articles