首页 > 解决方案 > 如何在 .core 中更改 Swagger 的基本 url 取决于请求

问题描述

在旧版本的 Swagger(.Net 框架)中,我使用了这个函数来更改 URL:

RootUrl(req => ComputeHostAsSeenByOriginalClient(req))

实际上我从请求中计算了基本 URL(我的应用程序位于负载均衡器后面)

新的 .core 方法是使用这个RoutePrefix

问题:
RoutePrefixProperty而不是Action,所以我没有HttpRequestMessage

这是ComputeHostAsSeenByOriginalClient完整的代码:

   public static string ComputeHostAsSeenByOriginalClient(HttpRequestMessage req)
        {
            var authority = req.RequestUri.Authority;
            var scheme = req.RequestUri.Scheme;



            if (req.Headers.Contains("X-Forwarded-Host"))
            {
                //we are behind a reverse proxy, use the host that was used by the client
                var xForwardedHost = req.Headers.GetValues("X-Forwarded-Host").First();
                //when multiple apache httpd are chained, each proxy append to the header 
                //with a comma (see //https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#x-headers).
                //so we need to take only the first host because it is the host that was 
                //requested by the original client.
                //note that other reverse proxies may behave differently but 
                //we are not taking care of them...
                var firstForwardedHost = xForwardedHost.Split(',')[0];
                authority = firstForwardedHost;
            }



            if (req.Headers.Contains("X-Forwarded-Proto"))
            {
                //now that we have the host, we also need to determine the protocol used by the 
                //original client.
                //if present, we are using the de facto standard header X-Forwarded-Proto
                //otherwise, we fallback to http
                //note that this is extremely brittle, either because the first proxy 
                //can "forget" to set the header or because another proxy can rewrite it...
                var xForwardedProto = req.Headers.GetValues("X-Forwarded-Proto").First();
                if (xForwardedProto.IndexOf(",") != -1)
                {
                    //when multiple apache, X-Forwarded-Proto is also multiple ...
                    xForwardedProto = xForwardedProto.Split(',')[0];
                }
                scheme = xForwardedProto;
            }
            //no reverse proxy mean we can directly use the RequestUri
            return scheme + "://" + authority;
        }

知道如何解决这个问题吗?

标签: c#asp.net-coreswaggerswashbuckleswashbuckle.aspnetcore

解决方案


尝试使用this.Request. 您可以从 访问请求对象Controller

        public string ComputeHostAsSeenByOriginalClient()
        {
            var req = this.Request;
            var scheme = req.Scheme;
            var authority = this.Request.Host.Value.ToString();

            if (req.Headers.ContainsKey("X-Forwarded-Host"))
            {
                //we are behind a reverse proxy, use the host that was used by the client
                StringValues xForwardedHost = "";
                if (req.Headers.TryGetValue("X-Forwarded-Host", out xForwardedHost))
                {
                    //when multiple apache httpd are chained, each proxy append to the header 
                    //with a comma (see //https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#x-headers).
                    //so we need to take only the first host because it is the host that was 
                    //requested by the original client.
                    //note that other reverse proxies may behave differently but 
                    //we are not taking care of them...
                    var firstForwardedHost = xForwardedHost.First().ToString().Split(',')[0];
                    authority = firstForwardedHost;
                }
            }

            if (req.Headers.ContainsKey("X-Forwarded-Proto"))
            {
                //now that we have the host, we also need to determine the protocol used by the 
                //original client.
                //if present, we are using the de facto standard header X-Forwarded-Proto
                //otherwise, we fallback to http
                //note that this is extremely brittle, either because the first proxy 
                //can "forget" to set the header or because another proxy can rewrite it...
                StringValues xForwardedProto = "";
                if (req.Headers.TryGetValue("X-Forwarded-Proto", out xForwardedProto))
                { 
                    if (xForwardedProto.First().ToString().IndexOf(",") != -1)
                    {
                        //when multiple apache, X-Forwarded-Proto is also multiple ...
                        xForwardedProto = xForwardedProto.First().ToString().Split(',')[0];
                    }
                    scheme = xForwardedProto;
                }
            }
            //no reverse proxy mean we can directly use the RequestUri
            return scheme + "://" + authority;

        }

推荐阅读