amateur
amateur

Reputation: 44655

get application url from current request

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

Answers (2)

Rahul
Rahul

Reputation: 77896

string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);

Upvotes: 4

Darin Dimitrov
Darin Dimitrov

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

Related Questions