谁能帮助我在获得ASP当前工作页面的URL。NET在c# ?


当前回答

如果您只想要http://和第一个斜杠之间的部分

string url = Request.Url.Host;

如果从此页调用将返回stackoverflow.com

下面是完整的分析

其他回答

如果你想要

localhost:2806 

http://localhost:2806/Pages/ 

然后使用:

HttpContext.Current.Request.Url.Authority

请求。Rawurl会给出当前页面的内容 它给出了您需要的确切路径

使用HttpContext.Current.Request.RawUrl

试试这个:

string url = HttpContext.Current.Request.Url.AbsoluteUri;
// http://localhost:1302/TESTERS/Default6.aspx

string path = HttpContext.Current.Request.Url.AbsolutePath;
// /TESTERS/Default6.aspx

string host = HttpContext.Current.Request.Url.Host;
// localhost

只是分享这是我的解决方案,感谢Canavar的帖子。

如果你有这样的东西:

"http://localhost:1234/Default.aspx?un=asdf&somethingelse=fdsa"

或者像这样:

"https://www.something.com/index.html?a=123&b=4567"

你只需要用户输入的部分,这样就可以了:

String strPathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
String strUrl = HttpContext.Current.Request.Url.AbsoluteUri.Replace(strPathAndQuery, "/");

这将导致以下结果:

"http://localhost:1234/"
"https://www.something.com/"

如果您只想要http://和第一个斜杠之间的部分

string url = Request.Url.Host;

如果从此页调用将返回stackoverflow.com

下面是完整的分析