如何在C#中获取当前页面的URL [重复]

本文翻译自:How to get the URL of the current page in C# [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

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.. 希望能帮助别人..

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值