How to call API in c# using angularjs
I usually solve this by using a factory
like this -
First in the .cshtml page I load all the angular js required.Then create a factory for the baseURL like this -
function(angular){ var module = angular.module('NameOfMyModule'); //gt the module module.factory('BaseUrl', function(){ return '@Url.Action("Action", "Controller")'; });}(window.angular);
Then inject that BaseURL
inside the controller -
....module.controller('SomeController', [...., 'BaseUrl', function(...., BaseUrl){ $scope.baseUrl = BaseUrl;}]);....`
Finally prepend it in the url
$http.get($scope.baseUrl + /...../).then(....);
I'm not sure if I undestood your question correctly, but I'm using angular constant to set server url
angular.constant("CONSTS", { "DEV_URL": "http://localhost:12345", "LIVE_URL": "http://server-ip/app"})
and then in $http call
$http.get(CONSTS.DEV_URL + '/api/controller/method?param=value'). then(function (response) { if (response.status == 200) { console.log(response.data); }});
I'm sure there is a way to automate this (gulp, grunt), but I didn't get there yet.When deploying the app I would just manually change the constant.If I'll find outomatic way to it I'll update the answer.
Hope this helps a bit...
I don't know your build process etc. but usually you can store application path in some constant value in Angular and use it when calling your API as a prefix.
If you have some kind of automated build, it is easy to prepare deployment packages with changed values(by using Gulp/Grunt/TeamCity/Octopus, whatever you like).