本文翻译自:How to get the URL of the current page in C# [duplicate]
This question already has an answer here: 这个问题在这里已有答案:
- Get URL of ASP.Net Page in code-behind [duplicate] 10 answers 在代码隐藏[重复] 10个答案中 获取ASP.Net页面的URL
Can anyone help out me in getting the URL of the current working page of ASP.NET in C#? 任何人都可以帮助我在C#中获取ASP.NET当前工作页面的URL吗?
#1楼
参考:https://stackoom.com/question/2URx/如何在C-中获取当前页面的URL-重复
#2楼
Just sharing as this was my solution thanks to Canavar's post. 因为这是我的解决方案,感谢Canavar的帖子。
If you have something like this: 如果您有这样的事情:
"http://localhost:1234/Default.aspx?un=asdf&somethingelse=fdsa"
or like this: 或者像这样:
"https://www.something.com/index.html?a=123&b=4567"
and you only want the part that a user would type in then this will work: 并且您只想要用户输入的部分然后这将起作用:
String strPathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
String strUrl = HttpContext.Current.Request.Url.AbsoluteUri.Replace(strPathAndQuery, "/");
which would result in these: 这将导致这些:
"http://localhost:1234/"
"https://www.something.com/"
#3楼
I guess its enough to return absolute path.. 我想它足以返回绝对路径..
Path.GetFileName( Request.Url.AbsolutePath )
using System.IO; 使用System.IO;
#4楼
If you want to get 如果你想得到
localhost:2806
from 从
http://localhost:2806/Pages/
then use: 然后使用:
HttpContext.Current.Request.Url.Authority
#5楼
You may at times need to get different values from URL. 您有时可能需要从URL获取不同的值。
Below example shows different ways of extracting different parts of URL 下面的示例显示了提取URL的不同部分的不同方法
EXAMPLE: (Sample URL) 示例:(示例网址)
http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QueryString2=2
CODE 码
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);
OUTPUT 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
You can copy paste above sample code & run it in asp.net web form application with different URL. 您可以复制上面示例代码的粘贴,并在具有不同URL的asp.net Web表单应用程序中运行它。
I also recommend reading ASP.Net Routing in case you may use ASP Routing then you don't need to use traditional URL with query string. 我还建议您阅读ASP.Net路由,以防您使用ASP路由,然后您不需要使用带有查询字符串的传统URL。
http://msdn.microsoft.com/en-us/library/cc668201%28v=vs.100%29.aspx http://msdn.microsoft.com/en-us/library/cc668201%28v=vs.100%29.aspx
#6楼
a tip for people who needs the path/url in global.asax file; 需要global.asax文件中路径/ url的人的提示;
If you need to run this in global.asax > Application_Start and you app pool mode is integrated then you will receive the error below: 如果您需要在global.asax> Application_Start中运行它并且您的应用程序池模式已集成,那么您将收到以下错误:
Request is not available in this context exception in Application_Start. Application_Start中的此上下文异常中不提供请求。
In that case you need to use this: 在这种情况下,您需要使用此:
System.Web.HttpRuntime.AppDomainAppVirtualPath System.Web.HttpRuntime.AppDomainAppVirtualPath
Hope will help others.. 希望能帮助别人..