0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That worked, thanks a lot. Would have given more up votes if I could. :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.