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

本文关键字:get http 自定义方法 方法 angularjs Web Net ASP Api 控制器 | 更新日期: 2023-09-27 18:37:02

我正在尝试使用 MVC 5、WEB API 2 和 AngularJS asp.net 学习和构建一个 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)如果我应该使用自定义方法,返回数据类型的方法结构是什么?

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

它非常简单。

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

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

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

例如;

假设我有处方控制器.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...
}

任何疑问都可以随时询问。