angularjs $http.get 在 ASP.Net Web Api 2 控制器中使用自定义方法的替代方法

Alternatives to using custom methods for angularjs $http.get in an ASP.Net Web Api 2 controller

我正在尝试使用 asp.net MVC 5、WEB API 2 和 AngularJS 学习和构建 Web 应用程序。我已经使用自定义 CRUD 操作构建了一个运行良好的应用程序。现在我想完全控制网络 api 控制器,这样我就可以 return 根据我的要求获取数据。例如,我想从以下代码中获取 returned 数据 -

        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) 除了在我的网络 api 控制器中创建自定义方法之外,还有什么方法可以检索 returned 数据吗? 2) 能不能稍微修改一下默认的方法?如果是这样,那又如何呢? 3) 如果我应该使用自定义方法,具有 returned 数据类型的方法结构是什么?

很简单。

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

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

IHttpActionResult 将是您在所有 HTTP 方法中的 return 类型,因为 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...

}

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