【问题标题】:How to rout to a web api when calling another web api调用另一个 web api 时如何路由到 web api
【发布时间】:2014-05-19 11:40:45
【问题描述】:

我有 ASP.Net web api2 odata 服务在我们的应用程序服务器中作为一个单独的项目运行。最初为了在我们的 Web 服务器应用程序中使用它,我们使用 Breeze.js 和 angular,后来我们得到了服务 URL 不能通过浏览器公开的限制(甚至没有在微风中声明为 javascript 变量)。

从概念上讲,我们建议采用另一种方法,即我们必须在 Web 服务器中创建另一个 api,微风将使用此 URL(不暴露实际的应用程序服务器 api)但 Web 服务器 api 应该路由实际的 odata 服务在应用服务器中

应用服务器

Asp.Net Web api 2 数据服务

网络服务器

API 服务(调用此服务路由作为单独服务运行的应用服务器)

Breeze.js: Entity Manager(应该调用Web服务器中的API服务)

App server api 被设计为在添加了微风属性的微风中消费。

任何帮助都会更受欢迎

【问题讨论】:

    标签: angularjs web-services breeze asp.net-web-api-routing


    【解决方案1】:

    我已经提出了在 Web 服务器级别设置代理 API 处理程序的解决方案,该处理程序在微风中作为服务公开,并且服务调用、发布和获取将使用此代理 Web api 进行管理。

    这里是示例

    在Web服务器API中,在APP_start/WebApiConfig.cs中添加了以下代码

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();
    
    
        //The fist value "*" needs to change with the actual redirection Uri    
            var cors = new EnableCorsAttribute("*", "*", "*", "DataServiceVersion, MaxDataServiceVersion");
            config.EnableCors(cors);
    
    
    
            config.Routes.MapHttpRoute(name: "Proxy", routeTemplate: "{*path}",
                                        handler: HttpClientFactory.CreatePipeline(innerHandler: new HttpClientHandler(),
                                        handlers: new DelegatingHandler[] { new ProxyHandler() }), defaults: new { path = RouteParameter.Optional },
                                        constraints: null);
        }
    
    }
    
    public class ProxyHandler : DelegatingHandler
    {
        protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
        {
            var pathandquery = request.RequestUri.PathAndQuery;
    
        // APPserver webapi URL
            var uri = "http:localhost/api/";
    
            request.RequestUri = uri;
    
            if (request.Method == HttpMethod.Get)
            {
                request.Content = null;
                return base.SendAsync(request, cancellationToken)
                       .ContinueWith<HttpResponseMessage>(t =>
                       {
                           var response = t.Result;
                           response.Headers.Add("Access-Control-Allow-Origin", "*");
    
                           return response;
                       });
            }
            else
            {
    
                if (request.Method == HttpMethod.Post)
                {
    
                    return base.SendAsync(request, cancellationToken)
                      .ContinueWith<HttpResponseMessage>(t =>
                      {
                          var response = t.Result;
                          response.Headers.Add("Access-Control-Allow-Origin", "*");
                          response.Headers.Add("Access-Control-Allow-Credentials", "true");
                          response.Headers.Add("Access-Control-Allow-Methods", "POST");
                          return response;
                      });
                }
    
                if (request.Method == HttpMethod.Put)
                {
    
                    return base.SendAsync(request, cancellationToken)
                      .ContinueWith<HttpResponseMessage>(t =>
                      {
                          var response = t.Result;
                          response.Headers.Add("Access-Control-Allow-Origin", "*");
                          response.Headers.Add("Access-Control-Allow-Credentials", "true");
                          response.Headers.Add("Access-Control-Allow-Methods", "PUT");
                          return response;
                      });
                }
            }
            return base.SendAsync(request, cancellationToken);
        }
    }
    

    公开这个 Web 服务器 API 将带来整个微风控制器元数据,而不会公开 appserver URL

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-01
      • 1970-01-01
      • 2019-03-30
      • 1970-01-01
      • 2018-03-15
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      相关资源
      最近更新 更多