C# 一次调用中的Request.Url.Host和ApplicationPath

C# 一次调用中的Request.Url.Host和ApplicationPath,c#,asp.net,code-behind,httpcontext,C#,Asp.net,Code Behind,Httpcontext,有没有办法在一次调用中获得HttpContext.Current.Request.Url.Host和HttpContext.Current.Request.ApplicationPath 类似“完整应用程序url”的东西 编辑:澄清-我需要的是[]内的部分: http://[www.mysite.com/mywebapp]/Pages/Default.aspx 我这样问纯粹是出于好奇 编辑2:谢谢你的回复,但是没有一个是我想要的。 仅供参考,我用这种方法解决了这个问题(但我仍有兴趣知道是否有更

有没有办法在一次调用中获得
HttpContext.Current.Request.Url.Host
HttpContext.Current.Request.ApplicationPath

类似“完整应用程序url”的东西

编辑:澄清-我需要的是[]内的部分:

http://[www.mysite.com/mywebapp]/Pages/Default.aspx
我这样问纯粹是出于好奇

编辑2:谢谢你的回复,但是没有一个是我想要的。 仅供参考,我用这种方法解决了这个问题(但我仍有兴趣知道是否有更平滑的方法):

检查:

如果无法直接从GetBaseUrl获得所需的内容,则可以执行以下操作:


GetAbsoluteUrl(HttpContext.Current.Request,“/”

感谢所有回复,但没有一个是我想要的。 仅供参考,我用这种方法解决了这个问题(但我仍有兴趣知道是否有更平滑的方法):


你真正应该做的是:

return String.Format("{0}://{1}/", Request.Url.Scheme, Request.Url.Host);

如果您使用的是HTTPS(或其他模式!),它就可以这样工作。

这在端口号为的本地主机上不起作用,因此进行了一些小的修改:

  private string GetWebAppRoot()
    {
        string host = (HttpContext.Current.Request.Url.IsDefaultPort) ? 
            HttpContext.Current.Request.Url.Host : 
            HttpContext.Current.Request.Url.Authority;
        host = String.Format("{0}://{1}", HttpContext.Current.Request.Url.Scheme, host);            
        if (HttpContext.Current.Request.ApplicationPath == "/")
            return host;
        else
            return host + HttpContext.Current.Request.ApplicationPath;
    }

谢谢,但是没有-这将为您提供调用它的ASPX页面的完整路径。我只需要主机+应用程序路径,而不是页面路径。IIRC SERVER_NAME可能无法获得所需的主机名-例如主机头解析()!如果我错了,请纠正我:)我已经在不同的网站上使用了多年了,它提供了请求的服务器名称,从http://到first/,如果可以的话,非常感谢。正在寻找一些可以与非标准端口和URL一起工作的东西,如。我将给它一个解释。我从我自己的帖子中删除了应答标记,并将其移动到这个位置,因为这个应答涵盖了更多的情况。是的,我的解决方案并不能真正处理这个问题,但这将:返回String.Format(“{0}://{1}/”,Request.Url.Scheme,Request.Url.Host);理论上,您需要Request.Url.Authority;主机没有端口。不幸的是,您最好使用Request.Headers[“HOST”]中的内容,因为它应该包含客户端用来连接的服务器的dns:端口。如果您担心HTTP/1.0请求,当然可以回到Request.Url.Host。我个人经历过Request.Url.Authority返回服务器软件绑定到的端口,而不是“主机”头中的端口,这是客户端用来连接的端口。(FWIW,它出现在SSRS 2012的Report Manager网站上。)
HttpContext.Current.Request.Url.AbsoluteUri
public static string GetSiteRoot()
{
  string port = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
  if (port == null || port == "80" || port == "443")
    port = "";
  else
    port = ":" + port;

  string protocol = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"];
  if (protocol == null || protocol == "0")
    protocol = "http://";
  else
    protocol = "https://";

  string sOut = protocol + System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + port + System.Web.HttpContext.Current.Request.ApplicationPath;

  if (sOut.EndsWith("/"))
  {
    sOut = sOut.Substring(0, sOut.Length - 1);
  }

  return sOut;
}
public string GetWebAppRoot()
{
    if(HttpContext.Current.Request.ApplicationPath == "/")
        return "http://" + HttpContext.Current.Request.Url.Host;
    else
        return "http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath;
}
return String.Format("{0}://{1}/", Request.Url.Scheme, Request.Url.Host);
  private string GetWebAppRoot()
    {
        string host = (HttpContext.Current.Request.Url.IsDefaultPort) ? 
            HttpContext.Current.Request.Url.Host : 
            HttpContext.Current.Request.Url.Authority;
        host = String.Format("{0}://{1}", HttpContext.Current.Request.Url.Scheme, host);            
        if (HttpContext.Current.Request.ApplicationPath == "/")
            return host;
        else
            return host + HttpContext.Current.Request.ApplicationPath;
    }