如何在 webapi 中将参数传递给数组 class?

How to pass parameters to array class in webapi?

我是 asp.net mvc 的新手 webapi.I 我正在创建一个 webapi service.In 我正在将参数作为数组发送 class.

以下是我的服务:

[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; }
}

现在如何将 passengercontactinfo 参数传递给上述服务。

webapiconfig 文件是否有任何变化? 我想像这样传递乘客详细信息:

passengername="pavan",
age="23",
Gender="M",

passengername="kumar",
Gender="M",
Age="22

如果你能为你的参数创建模型,那就更整洁了。要从客户端传递它们,您需要使用一种数据交换格式对它们进行格式化。我更喜欢使用 Newtonsoft.Json 库提供的 JSON。发送过程由 System.Net.Http 命名空间提供的 HttpClient class 处理。这是一些示例:

服务器端

    //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; }
        }

客户端

    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);
            }
    }

你可以这样做:

首先,由于您将两个对象作为参数传递,我们需要一个新的 class 来保存它们(因为我们只能将一个参数绑定到请求的内容):

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

现在是你的控制器(这只是一个测试控制器):

[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
        });
    }
}

您的请求应如下所示:

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 */
    }
}

请注意 api/addBusOrder 来自 RoutePrefix/Route 属性值的串联。