简体   繁体   English

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

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

I am trying to learn and build an web application using asp.net MVC 5, WEB API 2 and AngularJS. 我正在尝试使用asp.net MVC 5,WEB API 2和AngularJS学习和构建Web应用程序。 I have already built up a good working application with custom CRUD operations. 我已经使用自定义CRUD操作构建了一个运行良好的应用程序。 Now I want complete control on the web api controller, so that I can return data as per my requirement. 现在,我想对Web api控制器进行完全控制,以便可以根据需要返回数据。 For example I want to get the returned data from the following code - 例如,我想从以下代码中获取返回的数据-

        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;

I have three questions: 1) Is there any way to retrieve the returned data other than creating custom methods in my web api controller? 我有三个问题: 1)除了在Web api控制器中创建自定义方法外,还有什么方法可以检索返回的数据? 2) Can I just use the default method by modifying it a bit? 2)我可以通过稍微修改一下默认方法吗? if so then how? 如果可以的话如何 3) If I SHOULD use a custom method what would be the method structure with returned data type? 3)如果我应该使用自定义方法,返回数据类型的方法结构将是什么?

Its very simple. 非常简单。

Note: I don't understand your first question properly. 注意:我不正确地理解您的第一个问题。 for 2que and 3que.... 2que和3que ....

Lets say I'm using Get Method in Web api 2, MVC5 (I hope your are clear with HTTP methods in web api) Which collects required data (to be returned back to angularjs)..... Now it depends upon your requirement that how many objects you want to send back to client side. 可以说我在Web api 2中使用Get Method,MVC5(希望您对Web api中的HTTP方法很清楚),该方法收集所需的数据(返回给angularjs).....现在,这取决于您的要求您想要将多少个对象发送回客户端。

IHttpActionResult would be your return type in all HTTP methods as IHttpActionResult sends Status code via Web api.... IHttpActionResult将是所有HTTP方法中的返回类型,因为IHttpActionResult通过Web api发送状态代码。...

eg; 例如;

Lets say I have PrescriptionsController.cs. 可以说我有PrescriptionsController.cs。 So in it. 所以在里面。

[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.

}

Lets say I want to send two objects separately... then use following way, 假设我想分别发送两个对象...然后使用以下方式,

[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...

}

Any query feel free to ask. 任何查询都可以随意询问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 angular 和 asp.net web api 使用 http.get 下载 .xlsx 文件时出错 - Error downloading .xlsx file using http.get in angular with asp.net web api 所有方法都充当ASP.NET WEB API控制器中的HTTP动词(Get,Put,Post和Delete) - All methods act as a HTTP verb (Get, Put, Post And Delete) in ASP.NET WEB API controller 单个controller ASP.NET Web API中有多个GET方法 - Single controller with multiple GET methods in ASP.NET Web API 将$ http.get()方法中的$ scope.object传递给asp.net web api - pass $scope.object in $http.get() method to asp.net web api 在AngularJS $ http中传递日期到ASP.NET Web Api - Passing Date in AngularJS $http get to ASP.NET Web Api AngularJS $ http.post()将null发送到ASP.NET Web API控制器方法 - AngularJS $http.post() sends null to ASP.NET web API controller method 使用简单类型参数的AngularJS $ http.post到ASP.NET Web API控制器 - AngularJS $http.post to ASP.NET Web API controller with simple type argument Asp.net MVC项目中的Angularjs调用Web API控制器 - Angularjs in Asp.net MVC project calling web api controller 我可以在ASP.Net Web API控制器中使用多个Get方法 - Can I have Multiple Get Methods in ASP.Net Web API controller 使用实体框架的 ASP.Net Web Api 控制器方法语法 - ASP.Net Web Api Controller methods syntax using entity framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM