简体   繁体   English

如何基于域或主机应用程序动态更改Angularjs服务上的API端点

[英]How dynamically change API endpoint on Angularjs services based on the domain or host application

I have MVC web application that use Angularjs and a Web API, both application are published in different domains. 我有使用Angularjs和Web API的MVC Web应用程序,这两个应用程序都发布在不同的域中。 Everytime thet I commit code changes TFS publish both the MVC app and the Web API and using web.config transformation it change the web config connection and app keys to point to developer, production or testing. 每次我提交代码更改时,TFS都会发布MVC应用程序和Web API,并使用web.config转换它会更改Web配置连接和应用程序密钥以指向开发人员,生产或测试。

Now that I am using angular I have a service that point to the API endpoint to do something for instance the login. 现在我正在使用angular我有一个服务,指向API端点做一些事情,例如登录。

... ...

function loginService($http, $q) {

        var baseUri = "http://localhost/"

        var service = {
            login: login,
           ...
        };

        return service;


        function login(user, password) {
            var deferred = $q.defer();
            $http({ method: 'POST', url: baseUri + '/api/tokens',
...

As you can see the baseUri is set to localhost but I would like it have for the production environment something like var baseUri = "http://production/" and for testing baseUri = "http://testing/" . 正如您所看到的,baseUri设置为localhost,但我希望它对于生产环境类似于var baseUri = "http://production/"并且用于测试baseUri = "http://testing/" SO the baseUri changes depend of what is being publish by TFS. 所以baseUri的变化取决于TFS发布的内容。

Remember the the domain where the MVC application is hosted is different of the WebAPI so I cannot check the domain of the angular scope and build the base URI. 请记住,托管MVC应用程序的域与WebAPI不同,因此我无法检查角度范围的域并构建基URI。

Thank you 谢谢

Can you do a switch on the domain of the MVC app and based on whichever it is build the URI for the API? 你能否在MVC应用程序的域上进行切换并基于它为API构建URI的哪一个?

So something like: 所以类似于:

switch (MVCdomain) {
    case "http://production/": baseUri = "http://APIproduction/"; break;
    case "http://testing/": baseUri = "http://APItesting/"; break;
}

Could probably encapsulate that in its own endpointService that you can inject into your loginService or any other service that needs it, something like this: 可能会将其封装在自己的endpointService中,您可以将其注入loginService或任何其他需要它的服务,如下所示:

(function () {
    angular.module('app').factory('endpointService', function ($location) {
        return {
            getAPIEndpoint: function () {
                var endpoint = '';
                switch ($location.host()) {
                    // test if MVC app is in prod/test/local and determine what API endpoint should be called
                    case 'ProdDomainP.com': endpoint = 'ProdDomainAPIP.com'; break;
                    case 'TestDomainP.com': endpoint = 'TestDomainAPIP.com'; break;
                }
                return endpoint;
            }
        }
    });
})();

This would be a service you inject into other services to determine what API endpoint you call, like so: 这将是您注入其他服务的服务,以确定您调用的API端点,如下所示:

function loginService($http, $q, endpointService) {

    var baseUri = endpointService.getAPIEndoint();

    var service = {
        login: login,
       ...
    };

    return service;


    function login(user, password) {
        var deferred = $q.defer();
        $http({ method: 'POST', url: baseUri + '/api/tokens',
    ...

I am trying to do this as well. 我也想尝试这样做。 So I don't have to hard code api endpoints in my angular app, before I bootstrap angular, I make a service call in JavaScript to get endpoints. 所以我没有在我的角度应用程序中硬编码api端点,在我引导角度之前,我在JavaScript中进行服务调用以获取端点。 On the server side (web api) the controller reads from the web.config to get the end points that the angular app will use and returns them as JSON. 在服务器端(web api),控制器从web.config读取以获取角度应用程序将使用的端点,并将它们作为JSON返回。 Then , still before I bootstrap angular and after the endpoints are returned to JavaScript (via a native js promise or angular promise) , I set a global object var that holds all end points. 然后,在我引导角度之前,在端点返回到JavaScript之后(通过本机js promise或角度承诺),我设置了一个包含所有端点的全局对象var。 In angular there is an endpoint service that exposes this global end points object. 在角度中,有一个端点服务公开此全局端点对象。 Finally when that endpoint object is set, I bootstrap angular app. 最后,当设置端点对象时,我引导角度应用程序。 This ensures the endpoints are set before angular runs and tries tobuse the endpoints. 这可以确保在角度运行之前设置端点并尝试使用端点。

It sucks there needs to be this global endpoint object that exists outside of angular but it's the only way I can think of allows usage of web.config for transforms and not have to hardcode URL endpoint in js 它很糟糕,需要有一个存在于角度之外的全局端点对象,但这是我能想到的唯一方法,允许使用web.config进行转换,而不必在js中对URL端点进行硬编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MVC动态更改传入主机域上的stylebundle - mvc dynamically change stylebundle on incoming host domain 如何根据租户动态更改资源路径(我们正在根据主机名确定租户)? - How to change resource path dynamically based on the tenant(we are determining tenant based on host name)? 域驱动设计应用服务 - Domain Driven Design Application Services 如何在您的域中发布和托管MVC4应用程序? - How to publish and host a MVC4 application in your domain? 如何在MVC应用程序中不使用sodaclient的情况下与OpenData API端点通信 - How to talk to OpenData API Endpoint without using sodaclient in mvc application 将Azure网站应用程序托管到另一个域 - Host Azure website application to another domain 到域指向同一主机…将域1请求更改为域2 - to domains point to same host… change domain 1 requests to domain 2 了解域实体中的域服务与应用程序服务与行为(方法) - Understanding Domain Services vs. Application Services vs. Behavior (Methods) in Domain Entities 如何基于用户/凭据在Web API中动态显示控制器 - how to display controllers dynamically in web api based on user/credentials 如何将Windows服务托管到Windows Azure - How to host windows services to windows azure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM