如何在C#中获取当前页面的URL

632 c# asp.net

任何人都可以帮助我在C#中获取ASP.NET当前工作页面的URL吗?

Can*_*var 881

试试这个 :

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
Run Code Online (Sandbox Code Playgroud)

  • @drzaus如果`HttpContext.Current`为null,那么您没有处理页面请求,或者您在设置之前尝试访问它.如果您需要其他帮助,请开始一个新问题. (8认同)
  • 对于类似于Twitter用户帐户的路径,AbsolutePath似乎在.NET 3.5中返回"/"(未测试其他版本),例如http://twitter.com/#!/user.您可以使用Fragment方法获取磅(#)之后的任何内容. (4认同)
  • 实际上,当与UrlRewriter.net这样的重写机制一起使用时,这些都是不正确的,因为这将给出重写的URL.然后,您可以使用:Request.Url.GetLeftPart(UriPartial.Authority)+ Request.RawUrl (3认同)
  • 如果您需要在global.asax> Application_Start中运行它,并且您的应用程序池模式是"集成的",那么您将收到"请求在Application_Start中的此上下文异常中不可用"错误.在这种情况下,您需要使用System.Web.HttpRuntime.AppDomainAppVirtualPath (2认同)

Lea*_*ing 472

您有时可能需要从URL获取不同的值.

下面的示例显示了提取URL的不同部分的不同方法

示例:(示例网址)

http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QueryString2=2

Response.Write("<br/> " + HttpContext.Current.Request.Url.Host);
Response.Write("<br/> " + HttpContext.Current.Request.Url.Authority);
Response.Write("<br/> " + HttpContext.Current.Request.Url.Port);
Response.Write("<br/> " + HttpContext.Current.Request.Url.AbsolutePath);
Response.Write("<br/> " + HttpContext.Current.Request.ApplicationPath);
Response.Write("<br/> " + HttpContext.Current.Request.Url.AbsoluteUri);
Response.Write("<br/> " + HttpContext.Current.Request.Url.PathAndQuery);
Run Code Online (Sandbox Code Playgroud)

OUTPUT

localhost
localhost:60527
60527
/WebSite1test/Default2.aspx
/WebSite1test
http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QueryString1=2
/WebSite1test/Default2.aspx?QueryString1=1&QueryString2=2
Run Code Online (Sandbox Code Playgroud)

您可以复制上面示例代码的粘贴,并在具有不同URL的asp.net Web表单应用程序中运行它.

我还建议您阅读ASP.Net路由,以防您使用ASP路由,然后您不需要使用带有查询字符串的传统URL.

http://msdn.microsoft.com/en-us/library/cc668201%28v=vs.100%29.aspx


Soe*_*hay 105

因为这是我的解决方案,感谢Canavar的帖子.

如果您有这样的事情:

"http://localhost:1234/Default.aspx?un=asdf&somethingelse=fdsa"
Run Code Online (Sandbox Code Playgroud)

或者像这样:

"https://www.something.com/index.html?a=123&b=4567"
Run Code Online (Sandbox Code Playgroud)

并且您只想要用户输入的部分然后这将起作用:

String strPathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
String strUrl = HttpContext.Current.Request.Url.AbsoluteUri.Replace(strPathAndQuery, "/");
Run Code Online (Sandbox Code Playgroud)

这将导致这些:

"http://localhost:1234/"
"https://www.something.com/"
Run Code Online (Sandbox Code Playgroud)

  • 更简单的是:`Request.Url.GetLeftPart(UriPartial.Authority)`见这里:https://msdn.microsoft.com/en-us/library/system.uri.getleftpart(v=vs.110).aspx (15认同)

rom*_*n m 48

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

string url = Request.Url.Host;
Run Code Online (Sandbox Code Playgroud)

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

这是完整的细分

  • 不包括端口.需要:`HttpContext.Current.Request.Url.Port`. (12认同)
  • @JohnB或更确切地说是`HttpContext.Current.Request.Url.Authority`,通过一次调用获取`host:port` ... (2认同)

Ran*_*esh 22

request.rawurl将提供当前页面的内容,它提供了您所需的确切路径

使用 HttpContext.Current.Request.RawUrl


Tho*_*mas 14

如果你想得到

localhost:2806 
Run Code Online (Sandbox Code Playgroud)

http://localhost:2806/Pages/ 
Run Code Online (Sandbox Code Playgroud)

然后使用:

HttpContext.Current.Request.Url.Authority
Run Code Online (Sandbox Code Playgroud)


dvd*_*dmn 12

需要global.asax文件中路径/ url的人的提示;

如果您需要在global.asax> Application_Start中运行它并且您的应用程序池模式已集成,那么您将收到以下错误:

Application_Start中的此上下文异常中不提供请求.

在这种情况下,您需要使用此:

System.Web.HttpRuntime.AppDomainAppVirtualPath

希望能帮助别人..


Ben*_*son 9

搜索让我看到了这个页面,但这并不是我想要的.发布在这里以防其他人在这个页面上寻找我的土地.

如果您只有字符串值,有两种方法可以做到这一点.

.NET方式:

与@Canavar相同,但您可以实例化一个新的Uri对象

String URL = "http://localhost:1302/TESTERS/Default6.aspx";
System.Uri uri = new System.Uri(URL);
Run Code Online (Sandbox Code Playgroud)

这意味着您可以使用相同的方法,例如

string url = uri.AbsoluteUri;
// http://localhost:1302/TESTERS/Default6.aspx

string host = uri.host
// localhost
Run Code Online (Sandbox Code Playgroud)

正则表达方式:

获取URL的一部分(正则表达式)


May*_*hak 6

我想它足以返回绝对路径..

 Path.GetFileName( Request.Url.AbsolutePath )
Run Code Online (Sandbox Code Playgroud)

使用System.IO;