Reputation: 44655
I am writing a c# application.
I am accessing a page eg http://dev.mysite.com/page.aspx
How can I retrieve from the current context this http://dev.mysite.com/
I want to use this when creating url's in different environments so need to read it from the current request context.
Upvotes: 5
Views: 6285
Reputation: 1039278
Uri uri = new Uri("http://dev.mysite.com/page.aspx");
string authority = uri.GetLeftPart(UriPartial.Authority);
// authority will equal to http://dev.mysite.com
or if you are inside this page.aspx
you could directly use the Request.Url
property:
string authority = Request.Url.GetLeftPart(UriPartial.Authority);
Upvotes: 9