在 ASP.Net Web API2 中 angularjs 服务调用的 $http POST 请求期间使用条件 url

Using conditional urls during $http POST requests of angularjs service calls in ASP.Net Web API2

我现在通过以下方式发送请求。

在 PROD 中(托管在本地 IIS 服务器中)-

this.post = function (Prescription) {
    var request = $http({
        method: "post",
        dataType: "json",
        url: "/SmileMakersApp/SmileMakersApp/api/PrescriptionsAPI",
        data: Prescription
    });
    return request;
}

开发中 -

this.post = function (Prescription) {
    var request = $http({
        method: "post",
        dataType: "json",
        url: "/api/PrescriptionsAPI",
        data: Prescription
    });
    return request;
}

问题是我正在维护版本并通过 git 定期更新我的代码。因此,每当我对该文件进行新的更改时,我每次都必须解决冲突。类似这样的请求还有很多,解决每个请求的冲突很麻烦。有什么办法可以让我在 PROD 和 DEV 中定义不同的 url?我已经在后端做了类似的事情。

@if (HttpContext.Current.IsDebuggingEnabled)
{
    <base href="/">
}
else
{
    <base href="/SmileMakersApp/SmileMakersApp/">
}

如果我对你的问题理解正确,你要做的是配置不同的环境,比如开发、阶段和生产。由于您正在使用 Angular,因此您应该看看名为 angular-environement 的插件。

安装后,添加 environment 作为依赖项,并根据需要为每个环境设置域和变量。

angular.module('yourApp', ['environment']).
  config(function(envServiceProvider) {

  // this is where you need to set the domains and variables for each   environment 
  envServiceProvider.config({
    domains: {
      development: ['localhost', 'dev.local'],
      production: ['acme.com', 'acme.net', 'acme.org']
  },

  vars: {
    development: {
      apiUrl: '//localhost/api',
      staticUrl: '//localhost/static'
    },

    production: {
      apiUrl: '//api.acme.com/v2',
      staticUrl: '//static.acme.com'
    }               
  }
});

    // run the environment check, so the comparison is made 
    // before controllers and services are built 
    envServiceProvider.check();
});

设置完成后,您需要将 envService 注入您的控制器。然后您可以访问您的网址、变量等。

controller('SomeController', ['$scope', 'envService', function($scope, envService) {
    // gets the current environemt
    var environment = envService.get(); 

    // sets the desired environment
    envService.set('production');

    // gets the desired environment variable.
    var apiUrl = envService.read('apiUrl');
}]);

请注意,此代码尚未经过实际测试,但应该向您简要说明如何实施它。