2

I am new to asp.net mvc webapi.I am create one webapi service.In this service I am sending parameter as an array class.

Below is my service :

[AcceptVerbs("GET", "POST")]
public HttpResponseMessage addBusOrder(string UserUniqueID, int PlatFormID,
 string DeviceID, int RouteScheduleId,
 string JourneyDate, int FromCityid,
 int ToCityid, int TyPickUpID,
 Contactinfo Contactinfo, passenger[] pass)
{
 //done some work here
}
public class Contactinfo
{
 public string Name { get; set; }
 public string Email { get; set; }
 public string Phoneno { get; set; }
 public string mobile { get; set; }
}
public class passenger
{
 public string passengerName { get; set; }
 public string Age { get; set; }
 public string Fare { get; set; }
 public string Gender { get; set; }
 public string Seatno { get; set; }
 //public string Seattype { get; set; }
 // public bool Isacseat { get; set; }
}

Now how to pass passenger and contactinfo parameters to the above service.

Is there any changes in webapiconfig file? i want to pass passenger details like this:

passengername="pavan",
age="23",
Gender="M",
passengername="kumar",
Gender="M",
Age="22
asked Oct 25, 2015 at 6:40
9
  • @Webruster please see my code.and help me. Commented Oct 25, 2015 at 6:45
  • please any one help me.iam trying this last 2 days still I am not getting answer Commented Oct 25, 2015 at 7:09
  • Hi @Anil Kumar, are the passengers identified by passengerName? I mean, does each passenger have unique name? Commented Oct 25, 2015 at 7:19
  • @jhmt yes.each passenger have unque name Commented Oct 25, 2015 at 7:22
  • @jhmt I want to pass like this: passengername="pavan",Age="23",Gender="M",passengername="kumar",age="22",gender="m" Commented Oct 25, 2015 at 7:24

2 Answers 2

5

It will be much neater if you can create model of your parameter. To pass them from client side, you need to format them using one of data-interchange format. I prefer use JSON provided by Newtonsoft.Json library. Sending process is handled by HttpClient class provided by System.Net.Http namespace. Here is some sample:

Server Side

 //Only request with Post Verb that can contain body
 [AcceptVerbs("POST")]
 public HttpResponseMessage addBusOrder([FromBody]BusOrderModel)
 {
 //done some work here
 }
 //You may want to separate model into a class library so that server and client app can share the same model
 public class BusOrderModel
 {
 public string UserUniqueID { get; set; }
 public int PlatFormID { get; set; }
 public string DeviceID { get; set; }
 public int RouteScheduleId { get; set; }
 public string JourneyDate { get; set; }
 public int FromCityid { get; set; }
 public int ToCityid { get; set; }
 public int TyPickUpID { get; set; }
 public Contactinfo ContactInfo { get; set; }
 public passenger[] pass { get; set; }
 }

Client Side

 var busOrderModel = new BusOrderModel();
 var content = new StringContent(JsonConvert.SerializeObject(busOrderModel), Encoding.UTF8, "application/json");
 using (var handler = new HttpClientHandler())
 {
 using (HttpClient client = new HttpClient(handler, true))
 {
 client.BaseAddress = new Uri("yourdomain");
 client.DefaultRequestHeaders.Accept.Add(
 new MediaTypeWithQualityHeaderValue("application/json"));
 return await client.PostAsync(new Uri("yourdomain/controller/addBusOrder"), content);
 }
 }
answered Oct 25, 2015 at 7:46

Comments

1

Here's how you can do it:

First, since you are passing two objects as parameters we'll need a new class to hold them (because we can only bind one parameter to the request's content):

public class PassengersContact
{
 public Passenger[] Passengers { get; set; }
 public Contactinfo Contactinfo { get; set; }
}

and now for your controller (this is just a test controller):

[RoutePrefix("api")]
public class DefaultController : ApiController
{
 [HttpPost]
 // I prefer using attribute routing
 [Route("addBusOrder")]
 // FromUri means that the parameter comes from the uri of the request
 // FromBody means that the parameter comes from body of the request
 public IHttpActionResult addBusOrder([FromUri]string userUniqueId,
 [FromUri]int platFormId,
 [FromUri]string deviceId, [FromUri]int routeScheduleId,
 [FromUri]string journeyDate, [FromUri]int fromCityid,
 [FromUri]int toCityid, [FromUri]int tyPickUpId,
 [FromBody]PassengersContact passengersContact)
 {
 // Just for testing: I'm returning what was passed as a parameter
 return Ok(new
 {
 UserUniqueID = userUniqueId,
 PlatFormID = platFormId,
 RouteScheduleId = routeScheduleId,
 JourneyDate = journeyDate,
 FromCityid = fromCityid,
 ToCityid = toCityid,
 TyPickUpID = tyPickUpId,
 PassengersContact = passengersContact
 });
 }
}

Your request should look something like this:

POST http://<your server's URL>/api/addBusOrder?userUniqueId=a&platFormId=10&deviceId=b&routeScheduleId=11&journeyDate=c&fromCityid=12&toCityid=13&tyPickUpId=14
Content-Type: application/json
Content-Length: 110
{
 "passengers" : [{
 "passengerName" : "name",
 "age" : 52
 /* other fields go here */
 }
 ],
 "contactinfo" : {
 "name" : "contact info name",
 /* other fields go here */
 }
}

Notice the api/addBusOrder comes from concatenating the values of the RoutePrefix/Route attributes.

answered Oct 25, 2015 at 7:55

2 Comments

sometimes I want to send more passengers.then how to send passengers data dynamically
The passengers property is an array so you can pass as many passengers as you want in the Json array

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.