我需要能够得到充分网页的网址,我在来自用户的控制。它只是一个问题连接了一堆的请求变量在一起吗?如果是哪些?或者是有更多的simpiler的方式?

有帮助吗?

解决方案

我通常会使用 Request.Url.ToString() 得到充分的网址(包括查询),没有联需要。

其他提示

这是一个列表中我通常参照对于这种类型的信息:

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

我希望你会发现这很有用!

Request.Url.AbsoluteUri

这个酒店你需要的一切,都在一个简洁的呼吁。

请求。RawUrl

如果你需要完整的网址,一切从http的查询,你会需要连接以下变量

Request.ServerVariables("HTTPS") // to check if it's HTTP or HTTPS
Request.ServerVariables("SERVER_NAME") 
Request.ServerVariables("SCRIPT_NAME") 
Request.ServerVariables("QUERY_STRING")

对于 ASP.NET Core 你会需要拼出来:

@($"{Context.Request.Scheme}://{Context.Request.Host}{Context.Request.Path}{Context.Request.QueryString}")

或者你可以添加一个用语句你的观点:

@using Microsoft.AspNetCore.Http.Extensions

然后

@Context.Request.GetDisplayUrl()

_ViewImports.cshtml 可能是一个更好的地方, @using

更好的使用 Request.Url.OriginalStringRequest.Url.ToString() (根据 MSDN)

谢谢你们,我使用者两者的结合你的答案@基督教和@乔纳森我的特定需要。

"http://" + Request.ServerVariables["SERVER_NAME"] +  Request.RawUrl.ToString()

我不需要担心安全http需要的服务器名称的变量和RawUrl处理的路径从该域名,包括查询,如果存在的话。

如果你需要的口号还有,可以使用

Request.Url.Authority

例如:

string url = Request.Url.Authority + HttpContext.Current.Request.RawUrl.ToString();

if (Request.ServerVariables["HTTPS"] == "on")
{
    url = "https://" + url;
}
else 
{
    url = "http://" + url;
}

尝试下-

var FullUrl = Request.Url.AbsolutePath.ToString();
var ID = FullUrl.Split('/').Last();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top