Request.Url.Host和ApplicationPath在一个呼叫 [英] Request.Url.Host and ApplicationPath in one call

查看:363
本文介绍了Request.Url.Host和ApplicationPath在一个呼叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法让 HttpContext.Current.Request.Url.Host HttpContext.Current.Request.ApplicationPath 在一个电话?

像全面应用的URL?

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

 的http:// [www.mysite.com/mywebapp] /Pages/Default.aspx

我问只是出于好奇。

编辑2:感谢所有的答复,但他们都不是正是我一直在寻找。
仅供参考,我的问题解决了这样的(但我还是想知道,如果有一个平滑的方式):

 公共字符串GetWebAppRoot()
{
    如果(HttpContext.Current.Request.ApplicationPath ==/)
        回归的http://+ HttpContext.Current.Request.Url.Host;
    其他
        回归的http://+ HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath;
}


解决方案

 公共静态字符串GetSiteRoot()
{
  字符串端口= System.Web.HttpContext.Current.Request.ServerVariables [SERVER_PORT];
  如果(端口== NULL ||端口==80||端口==443)
    端口=;
  其他
    端口=:+端口;  串协议= System.Web.HttpContext.Current.Request.ServerVariables [SERVER_PORT_SECURE];
  如果(协议==空||协议==0)
    协议=HTTP://;
  其他
    协议=htt​​ps://开头;  字符串SOUT = +协议System.Web.HttpContext.Current.Request.ServerVariables [SERVER_NAME] +端口+ System.Web.HttpContext.Current.Request.ApplicationPath;  如果(sO​​ut.EndsWith(/))
  {
    SOUT = sOut.Substring(0,sOut.Length - 1);
  }  返回SOUT;
}

Is there any way to get HttpContext.Current.Request.Url.Host and HttpContext.Current.Request.ApplicationPath in one call?

Something like "full application url"?

EDIT: Clarification - what I need is this the part within []:

http://[www.mysite.com/mywebapp]/Pages/Default.aspx

I ask simply out of curiosity.

EDIT 2: Thanks for all the replies, but none of them were exactly what I was looking for. FYI, I solved the problem this way (but am still interested in knowing if there's a smoother way):

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;
}

解决方案

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;
}

这篇关于Request.Url.Host和ApplicationPath在一个呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆