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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/593709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 09:36:09  来源:igfitidea点击:

How to get the URL of the current page in C#

c#asp.net

提问by

Can anyone help out me in getting the URL of the current working page of ASP.NET in C#?

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

回答by Canavar

Try this :

尝试这个 :

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

回答by roman m

if you just want the part between http:// and the first slash

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

string url = Request.Url.Host;

would return stackoverflow.com if called from this page

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

Here's the complete breakdown

这是完整的细分

回答by Ben Pearson

A search landed me at this page, but it wasn't quite what I was looking for. Posting here in case someone else looking for what I was lands at this page too.

搜索使我进入了这个页面,但这并不是我想要的。在这里发帖以防其他人也在此页面上寻找我的内容。

There is two ways to do it if you only have a string value.

如果您只有一个字符串值,有两种方法可以做到。

.NET way:

.NET方式:

Same as @Canavar, but you can instantiate a new Uri Object

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

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

which means you can use the same methods, e.g.

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

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

string host = uri.host
// localhost

Regex way:

正则表达式方式:

Getting parts of a URL (Regex)

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

回答by Soenhay

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/"

回答by Mayank Pathak

I guess its enough to return absolute path..

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

 Path.GetFileName( Request.Url.AbsolutePath )

using System.IO;

使用 System.IO;

回答by Thomas Amar

If you want to get

如果你想得到

localhost:2806 

from

http://localhost:2806/Pages/ 

then use:

然后使用:

HttpContext.Current.Request.Url.Authority

回答by Learning

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

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

输出

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 Routing,以防您可能使用 ASP Routing,然后您就不需要使用带有查询字符串的传统 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

回答by dvdmn

a tip for people who needs the path/url in global.asax file;

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

If you need to run this in global.asax > Application_Startand you app pool mode is integratedthen 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..

希望能帮助别人..

回答by Randhi Rupesh

the request.rawurl will gives the content of current page it gives the exact path that you required

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

use HttpContext.Current.Request.RawUrl

HttpContext.Current.Request.RawUrl