Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I am trying to learn and build an web application using asp.net MVC 5, WEB API 2 and AngularJS. I have already built up a good working application with custom CRUD operations. Now I want complete control on the web api controller, so that I can return data as per my requirement. 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? 2) Can I just use the default method by modifying it a bit? if so then how? 3) If I SHOULD use a custom method what would be the method structure with returned data type?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Its very simple.

Note: I don't understand your first question properly. for 2que and 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.

IHttpActionResult would be your return type in all HTTP methods as IHttpActionResult sends Status code via Web api....

eg;

Lets say I have 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.

share|improve this answer
    
That worked, thanks a lot. Would have given more up votes if I could. :D – Shuvro Jan 11 at 18:32
    
Oh thanks. enjoy! No problem... – micronyks Jan 12 at 3:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.