get application url from current request
I am writing a c# application.
I am accessing a page eg http://dev.mysite.com/page.a开发者_StackOverflowspx
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.
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);
string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);
精彩评论