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


当前回答

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

string url = Request.Url.Host;

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

下面是完整的分析

其他回答

给那些需要全局路径/url的人一个提示。asax文件;

如果你需要全局运行。asax > Application_Start和你的应用池模式是集成的,然后你会收到以下错误:

请求在此上下文异常中不可用 Application_Start。

在这种情况下,你需要使用这个:

System.Web.HttpRuntime.AppDomainAppVirtualPath

希望能帮助别人。

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

使用HttpContext.Current.Request.RawUrl

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

string url = Request.Url.Host;

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

下面是完整的分析

试试这个:

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

如果你想要

localhost:2806 

http://localhost:2806/Pages/ 

然后使用:

HttpContext.Current.Request.Url.Authority