简体   繁体   中英

Automatic “proxy” creation for ASP.NET MVC Controller for AngularJs service

When I'm using a ASP.NET MVC controller with AngularJS, then my controller mostly contains JSON result functions. And then when I create my AngularJs service its allways the same code to write the service for 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 automaticly created 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've written my own little "proxy" (don't know if this is the right naming) which automaticly creates a angularJS service for my controller JSON result functions as new javaScript file - that works fine so far.

my question are:

Whats the right way to handle this task is there some GitHub Projekt out there which supports this allready or how are you solving this "problem"?

Whats the right naming because I can't find anything about this solution and I don't know if "proxy" is right?

I've created my own GitHub Repo Using T4 Template to create the Proxy Functions

https://github.com/squadwuschel/MvcControllerToProxyGenerator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM