【问题标题】:Alternatives to using custom methods for angularjs $http.get in an ASP.Net Web Api 2 controller在 ASP.Net Web Api 2 控制器中为 angularjs $http.get 使用自定义方法的替代方法
【发布时间】:2015-01-11 15:33:13
【问题描述】:

我正在尝试使用 asp.net MVC 5、WEB API 2 和 AngularJS 学习和构建 Web 应用程序。我已经使用自定义 CRUD 操作构建了一个良好的工作应用程序。现在我想完全控制 web api 控制器,以便我可以根据我的要求返回数据。比如我想从下面的代码中获取返回的数据-

        string today = DateTime.Now.ToString("dd/MM/yyyy");
        var appointment1 = from prescription in db.Prescriptions
                           where prescription.appointment == "15/01/2015"
                           from consultation in prescription.Consultations

                           select new
                           {
                               ID = prescription.id,
                               Name = prescription.patient_name,
                               Contact = prescription.patient_contact,
                               Task = prescription.next_task
                           };

        var appointment2 = from consultation in db.Consultations
                           where consultation.next_date == "15/01/2015"
                           select new
                           {
                               ID = consultation.Prescription.id,
                               Name = consultation.Prescription.patient_name,
                               Contact = consultation.Prescription.patient_contact,
                               Task = consultation.next_task
                           };

        var finalAppointments = appointment1.Concat(appointment2);
        return finalAppointments;

我有三个问题: 1) 除了在我的 web api 控制器中创建自定义方法之外,还有什么方法可以检索返回的数据? 2) 我可以通过稍微修改一下使用默认方法吗?如果是这样,那怎么办? 3) 如果我应该使用自定义方法,返回数据类型的方法结构是什么?

【问题讨论】:

    标签: c# asp.net asp.net-mvc angularjs asp.net-web-api


    【解决方案1】:

    它非常简单。

    注意:我没有正确理解您的第一个问题。对于 2que 和 3que....

    假设我在 Web api 2,MVC5 中使用 Get 方法(我希望您对 Web api 中的 HTTP 方法很清楚) 它收集所需的数据(返回给 angularjs)..... 现在取决于您希望将多少对象发送回客户端。

    IHttpActionResult 将是您在所有 HTTP 方法中的返回类型,因为 IHttpActionResult 通过 Web api 发送状态代码......

    例如;

    假设我有 PrescriptionsController.cs。所以在里面。

    [HttpGet]
    public IHttpActionResult Get()
    {
          var appointment1 = from prescription in db.Prescriptions
                           where prescription.appointment == "15/01/2015"
                           from consultation in prescription.Consultations
    
                           select new
                           {
                               ID = prescription.id,
                               Name = prescription.patient_name,
                               Contact = prescription.patient_contact,
                               Task = prescription.next_task
                           };
    
        var appointment2 = from consultation in db.Consultations
                           where consultation.next_date == "15/01/2015"
                           select new
                           {
                               ID = consultation.Prescription.id,
                               Name = consultation.Prescription.patient_name,
                               Contact = consultation.Prescription.patient_contact,
                               Task = consultation.next_task
                           };
    
        var finalAppointments = appointment1.Concat(appointment2);
       // return finalAppointments;    //rather I'd use below line...
    
          return Ok(finalAppointments); // here your are concatenating two objects and want to send single object only.
    
    }
    

    假设我想分别发送两个对象... 然后使用以下方式,

    [HttpGet]
    //public IHttpActionResult Get()
    public dynamic Get()
    {
          var appointment1 = from prescription in db.Prescriptions
                           where prescription.appointment == "15/01/2015"
                           from consultation in prescription.Consultations
    
                           select new
                           {
                               ID = prescription.id,
                               Name = prescription.patient_name,
                               Contact = prescription.patient_contact,
                               Task = prescription.next_task
                           };
    
        var appointment2 = from consultation in db.Consultations
                           where consultation.next_date == "15/01/2015"
                           select new
                           {
                               ID = consultation.Prescription.id,
                               Name = consultation.Prescription.patient_name,
                               Contact = consultation.Prescription.patient_contact,
                               Task = consultation.next_task
                           };
    
        //var finalAppointments = appointment1.Concat(appointment2);
       // return finalAppointments;    //rather I'd use below line...
    
       //   return Ok(finalAppointments); // here your are concatenating two objects.
    
       return new {appointment1 ,appointment2 }; // you can send multiple objects...
    
    }
    

    如有任何疑问,请随时提出。

    【讨论】:

    • 成功了,非常感谢。如果可以的话,我会放弃更多的选票。 :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-23
    • 1970-01-01
    相关资源
    最近更新 更多