从当前请求获取应用程序URL

时间:2011-06-18 16:25:18

标签: c# request httprequest httpcontext

我正在写一个c#应用程序。

我正在访问一个页面,例如http://dev.mysite.com/page.aspx

如何从当前上下文中检索此http://dev.mysite.com/

我想在不同环境中创建url时使用它,因此需要从当前请求上下文中读取它。

2 个答案:

答案 0 :(得分:9)

Uri uri = new Uri("http://dev.mysite.com/page.aspx");
string authority = uri.GetLeftPart(UriPartial.Authority);
// authority will equal to http://dev.mysite.com

或者如果你在page.aspx内,你可以直接使用Request.Url属性:

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

答案 1 :(得分:4)

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