如何在 C# 中获取我所在页面的完整 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40680/
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
How do I get the full url of the page I am on in C#
提问by RedWolves
I need to be able to get at the full URL of the page I am on from a user control. Is it just a matter of concatenating a bunch of Request variables together? If so which ones? Or is there a more simpiler way?
我需要能够从用户控件获取我所在页面的完整 URL。是否只是将一堆 Request 变量连接在一起的问题?如果有,是哪些?或者有更简单的方法吗?
采纳答案by travis
I usually use Request.Url.ToString()
to get the full url (including querystring), no concatenation required.
我通常Request.Url.ToString()
用来获取完整的 url(包括查询字符串),不需要连接。
回答by FlySwat
Request.RawUrl
请求.RawUrl
回答by Christian Hagelid
if you need the full URL as everything from the http to the querystring you will need to concatenate the following variables
如果您需要完整的 URL 作为从 http 到查询字符串的所有内容,您将需要连接以下变量
Request.ServerVariables("HTTPS") // to check if it's HTTP or HTTPS
Request.ServerVariables("SERVER_NAME")
Request.ServerVariables("SCRIPT_NAME")
Request.ServerVariables("QUERY_STRING")
回答by RedWolves
Thanks guys, I used a combination of both your answers @Christian and @Jonathan for my specific need.
谢谢你们,我结合了你的答案@Christian 和@Jonathan 来满足我的特定需求。
"http://" + Request.ServerVariables["SERVER_NAME"] + Request.RawUrl.ToString()
I don't need to worry about secure http, needed the servername variable and the RawUrl handles the path from the domain name and includes the querystring if present.
我不需要担心安全的 http,需要 servername 变量和 RawUrl 处理来自域名的路径并包括查询字符串(如果存在)。
回答by DevelopingChris
Request.Url.AbsoluteUri
This property does everything you need, all in one succinct call.
此属性可在一个简洁的调用中完成您需要的一切。
回答by IonB
If you need the port number also, you can use
如果您还需要端口号,则可以使用
Request.Url.Authority
Example:
例子:
string url = Request.Url.Authority + HttpContext.Current.Request.RawUrl.ToString();
if (Request.ServerVariables["HTTPS"] == "on")
{
url = "https://" + url;
}
else
{
url = "http://" + url;
}
回答by Mohsen
Here is a list I normally refer to for this type of information:
这是我通常参考的此类信息的列表:
Request.ApplicationPath : /virtual_dir
Request.CurrentExecutionFilePath : /virtual_dir/webapp/page.aspx
Request.FilePath : /virtual_dir/webapp/page.aspx
Request.Path : /virtual_dir/webapp/page.aspx
Request.PhysicalApplicationPath : d:\Inetpub\wwwroot\virtual_dir\
Request.QueryString : /virtual_dir/webapp/page.aspx?q=qvalue
Request.Url.AbsolutePath : /virtual_dir/webapp/page.aspx
Request.Url.AbsoluteUri : http://localhost:2000/virtual_dir/webapp/page.aspx?q=qvalue
Request.Url.Host : localhost
Request.Url.Authority : localhost:80
Request.Url.LocalPath : /virtual_dir/webapp/page.aspx
Request.Url.PathAndQuery : /virtual_dir/webapp/page.aspx?q=qvalue
Request.Url.Port : 80
Request.Url.Query : ?q=qvalue
Request.Url.Scheme : http
Request.Url.Segments : /
virtual_dir/
webapp/
page.aspx
Hopefully you will find this useful!
希望你会发现这很有用!
回答by Eni
Better to use Request.Url.OriginalString
than Request.Url.ToString()
(according to MSDN)
Request.Url.OriginalString
比Request.Url.ToString()
(根据MSDN)更好用
回答by Serj Sagan
For ASP.NET Core
you'll need to spell it out:
因为ASP.NET Core
你需要把它拼出来:
@($"{Context.Request.Scheme}://{Context.Request.Host}{Context.Request.Path}{Context.Request.QueryString}")
Or you can add a using statement to your view:
或者您可以在视图中添加 using 语句:
@using Microsoft.AspNetCore.Http.Extensions
then
然后
@Context.Request.GetDisplayUrl()
The _ViewImports.cshtml
might be a better place for that @using
这_ViewImports.cshtml
可能是一个更好的地方@using
回答by Abhishek Kanrar
Try the following -
尝试以下 -
var FullUrl = Request.Url.AbsolutePath.ToString();
var ID = FullUrl.Split('/').Last();