如何在c# [duplicate]中获取当前页面的URL

时间:2021-08-02 23:44:32

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#?

谁能帮我获取ASP当前工作页面的URL ?网络在c#中?

9 个解决方案

#1


807  

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

#2


392  

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)

例如:(示例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.

您可以复制上面的示例代码并在asp.net web表单应用程序中使用不同的URL运行它。

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。如果您可能使用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

#3


89  

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

#4


41  

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

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

string url = Request.Url.Host;

would return *.com if called from this page

如果从这个页面调用,会返回*.com吗

Here's the complete breakdown

这是完全崩溃

#5


17  

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

请求。rawurl将给出当前页面的内容,它将给出所需的确切路径

use HttpContext.Current.Request.RawUrl

使用HttpContext.Current.Request.RawUrl

#6


13  

If you want to get

如果你想要

localhost:2806 

from

http://localhost:2806/Pages/ 

then use:

然后使用:

HttpContext.Current.Request.Url.Authority

#7


10  

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

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

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:

如果需要在全局环境中运行。asax > Application_Start与您的app池模式集成后,您将收到以下错误:

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..

希望能够帮助别人。

#8


8  

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的一部分(Regex)

#9


6  

I guess its enough to return absolute path..

2 .我想这足以补偿我的损失。

 Path.GetFileName( Request.Url.AbsolutePath )

using System.IO;

使用先;

#1


807  

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

#2


392  

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)

例如:(示例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.

您可以复制上面的示例代码并在asp.net web表单应用程序中使用不同的URL运行它。

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。如果您可能使用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

#3


89  

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

#4


41  

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

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

string url = Request.Url.Host;

would return *.com if called from this page

如果从这个页面调用,会返回*.com吗

Here's the complete breakdown

这是完全崩溃

#5


17  

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

请求。rawurl将给出当前页面的内容,它将给出所需的确切路径

use HttpContext.Current.Request.RawUrl

使用HttpContext.Current.Request.RawUrl

#6


13  

If you want to get

如果你想要

localhost:2806 

from

http://localhost:2806/Pages/ 

then use:

然后使用:

HttpContext.Current.Request.Url.Authority

#7


10  

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

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

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:

如果需要在全局环境中运行。asax > Application_Start与您的app池模式集成后,您将收到以下错误:

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..

希望能够帮助别人。

#8


8  

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的一部分(Regex)

#9


6  

I guess its enough to return absolute path..

2 .我想这足以补偿我的损失。

 Path.GetFileName( Request.Url.AbsolutePath )

using System.IO;

使用先;