Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net Absolute Path of a URL

To make it simpler for a webapp to share files with another app on a different server, I'm using a base href tag in my master page. As many people have discovered, this breaks webform paths. I have a working Form Adaptor class but am not sure how to get the absolute path of the url. Currently, my program is hardcoded to use something akin to :

HttpContext Context = HttpContext.Current; value = "http://localhost" + Context.Request.RawUrl; 

It is worth noting that I'm currently testing on my local IIS server, so there's a strange tendency for a lot of things I've tried using in order what the absolute path do not include the domain name (my local IIS is not visible externally). Which means it isn't an absolute path and thus the base href will wreck it.

Is there a good way to handle this such that it will work locally without hardcoding but will also work properly when uploaded to a server? I'd prefer to avoid anything that involves doing something different on the server-side copy.

Yes, I realize I could use separate web.config files locally and on the server to get this information but this is ugly and violates DRY.

like image 889
Brian Avatar asked Feb 19 '09 22:02

Brian


People also ask

What is a URL absolute path?

What is an absolute URL? An absolute URL is the full URL, including protocol ( http / https ), the optional subdomain (e.g. www ), domain ( example.com ), and path (which includes the directory and slug). Absolute URLs provide all the available information to find the location of a page.

How do you make an absolute URL?

If you prefix the URL with // it will be treated as an absolute one. For example: <a href="//google.com">Google</a> . Keep in mind this will use the same protocol the page is being served with (e.g. if your page's URL is https://path/to/page the resulting URL will be https://google.com ).

What is absolute path and relative path in URL?

An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory. Relative path. Relative path is defined as the path related to the present working directly(pwd) ...


2 Answers

I have used this in the past:

// Gets the base url in the following format:  // "http(s)://domain(:port)/AppPath" HttpContext.Current.Request.Url.Scheme      + "://"     + HttpContext.Current.Request.Url.Authority      + HttpContext.Current.Request.ApplicationPath; 
like image 51
John Rasch Avatar answered Oct 17 '22 08:10

John Rasch


Old post but here is another slightly less verbose method

var baseUri = new Uri(HttpContext.Current.Request.Url, "/"); 
like image 36
Chris Foster Avatar answered Oct 17 '22 06:10

Chris Foster