Whenever I utilize an ASP.NET MVC controller with AngularJS, the majority of my controller functions consist of JSON results. When creating my AngularJS service, I find myself repeatedly writing similar code to handle GET or POST calls to my ASP.NET controller functions.
MVC Home Controller Functions:
[AngularCreateProxy]
public JsonResult InitUnitTestPersonEntry()
{
return Json(new PersonEntry(), JsonRequestBehavior.AllowGet);
}
[AngularCreateProxy]
public JsonResult InitUnitTestSearchModel()
{
PersonSearchModel searchModel = new PersonSearchModel() {Name = String.Empty};
return Json(searchModel, JsonRequestBehavior.AllowGet);
}
[AngularCreateProxy]
public JsonResult AddUnitTestPerson(PersonEntry entry)
{
PersonEntries.Add(entry);
return Json(entry, JsonRequestBehavior.AllowGet);
}
My automatically generated AngularJS Home Service:
function homePSrv($http, $log) {
this.log = $log, this.http = $http;
}
homePSrv.prototype.InitUnitTestPersonEntry = function () {
return this.http.get('/Home/InitUnitTestPersonEntry').then(function (result) {
return result.data;
});
}
homePSrv.prototype.InitUnitTestSearchModel = function () {
return this.http.get('/Home/InitUnitTestSearchModel').then(function (result) {
return result.data;
});
}
homePSrv.prototype.AddUnitTestPerson = function (entry) {
return this.http.post('/Home/AddUnitTestPerson',entry).then(function (result) {
return result.data;
});
}
angular.module("app.homePSrv", [])
.service("homePSrv", ['$http', '$log', homePSrv]);
I have developed my own "proxy" (unsure if this is the correct term) that automatically generates an AngularJS service for my controller's JSON result functions as a new JavaScript file - the process has been successful so far.
My questions are:
What is the optimal approach for handling this task? Is there an existing GitHub project that already addresses this issue, or how do you tackle this challenge?
What is the appropriate terminology for this solution since I have not found any relevant information on it and am uncertain whether "proxy" is accurate?