Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在反向代理之后获取应用程序基url_C#_Asp.net Mvc_Asp.net Web Api_Reverse Proxy_Urlhelper - Fatal编程技术网

C# 如何在反向代理之后获取应用程序基url

C# 如何在反向代理之后获取应用程序基url,c#,asp.net-mvc,asp.net-web-api,reverse-proxy,urlhelper,C#,Asp.net Mvc,Asp.net Web Api,Reverse Proxy,Urlhelper,ASP.NET应用程序是否有任何方法可以在通过反向代理/网关的请求上下文中导出其url路径和主机名,从而知道自己的路由 通过网关请求此url时: http://conoso.com/behind/some/reverseproxy/api/values/ 网关正在将请求重新路由到其他位置: http://10.0.0.0/api/values 它正在使用DefaultApi路由访问以下ApiController ValuesController的Get方法: // GET api/value

ASP.NET应用程序是否有任何方法可以在通过反向代理/网关的请求上下文中导出其url路径和主机名,从而知道自己的路由

通过网关请求此url时:

http://conoso.com/behind/some/reverseproxy/api/values/
网关正在将请求重新路由到其他位置:

http://10.0.0.0/api/values
它正在使用DefaultApi路由访问以下ApiController ValuesController的Get方法:

// GET api/values
public HttpResponseMessage Get()
{
  var res = Request.CreateResponse();
  res.Headers.Add(
    "Location",
    Url.Link(
      "DefaultApi",
      new
        {
          id = 1
        }));
  return res;
}
它返回以下标题值:

Location: http://10.0.0.0/api/values/1
当我希望发送以下有效路径时:

Location: http://conoso.com/behind/some/reverseproxy/api/values/1
在框架中使用内置方法的替代方法是手动格式化字符串:

var baseUrl = "http://conoso.com/behind/some/reverseproxy";
string.Format("{0}/values/1", baseUrl);

但这会散发出一些邪恶的代码气味。有没有人能推荐一种更干净的方法?

我们的secure entry服务器背后也存在同样的问题。 对于REST调用,我们希望在服务器端生成URL,使它们在java脚本中可用。但它们不包括安全入口服务器添加的子路径

因此,我们提出了这样一种解决方法(在布局页面中呈现):


baseUrl=document.getElementById('urlBase').getAttribute('href');
href=“/”
已被entry server替换为
href=“/path/”
,在访问REST服务时,我们可以轻松地将
baseUrl
与我们的相对路径连接起来

希望对你的情况也有帮助


(我发布了相同的答案,希望可以复制和粘贴)

我假设您使用
Url.Content(“~”
而不是
Url.Link
?@C.Barlow得到类似的结果,但在MVC控制器中不同,但它只返回应用程序中的相对路径,即/api/values/1(使用上面的示例)。使用反向代理网关,这将解决url路径太远而无法正确路由的问题。。。其中一些有点老套,但可能会满足您的需要:
<a id="urlBase" href="/" style="display: none;"></a>
<script type="text/javascript">
    baseUrl = document.getElementById('urlBase').getAttribute('href');
</script>