javascript - 为 AngularJs 服务的 ASP.NET MVC Controller 自动创建 "proxy"

标签 javascript asp.net asp.net-mvc angularjs

当我将 ASP.NET MVC Controller 与 AngularJS 一起使用时,我的 Controller 主要包含 JSON 结果函数。然后,当我创建 AngularJs 服务时,它始终使用相同的代码来编写服务,以对 ASP.NET Controller 函数进行 Get 或 POST 调用。

MVC Home Controller 功能:

[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);
}

我自动创建的 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]);

我已经编写了自己的小“代理”(不知道这是否是正确的命名),它会自动为我的 Controller JSON 结果函数创建一个 angularJS 服务作为新的 javaScript 文件 - 到目前为止工作正常。 p>

我的问题是:

处理此任务的正确方法是什么?是否有一些 GitHub Projekt 已经支持此任务,或者您如何解决这个“问题”?

正确的命名是什么,因为我找不到有关此解决方案的任何内容,并且我不知道“代理”是否正确?

最佳答案

我使用 T4 模板创建了自己的 GitHub 存储库来创建代理函数

https://github.com/squadwuschel/MvcControllerToProxyGenerator

关于javascript - 为 AngularJs 服务的 ASP.NET MVC Controller 自动创建 "proxy",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29929433/

相关文章:

c# - 全局覆盖 ASP.Net MVC 默认属性错误消息

asp.net-mvc - ASP MVC 3 cookie 丢失 HttpOnly 和 Secure 标志

javascript - 我正在尝试将函数传递给我创建的指令,但它在 angularjs 中不起作用

javascript - ASP .NET 打印 iFrame

javascript - Angular 2 的 IE 错误

asp.net - 可编程、安全的 FTP 替代

asp.net - 使用 ASP.NET 5 中的默认 DI 容器一次性注册所有服务,类似于 Autofac

c# - @Html.HiddenFor(x => x.Id) 不起作用,但 <input type ="hidden"value = @Model.Id> 有效。为什么?

php - 一个用户如何通过网站从另一个用户的计算机下载文件?

javascript - 使用 bcryptjs 进行密码哈希处理