如何在webapi中传递参数给数组类
本文关键字:参数 数组 webapi | 更新日期: 2023-09-27 18:19:11
我是asp.net mvc webapi的新手。我正在创建一个webapi服务。在此服务中,我将参数作为数组类发送。
以下是我的服务:
[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; }
}
现在如何将passenger
和contactinfo
参数传递给上述服务
webapiconfig文件有任何变化吗?我想传递这样的乘客信息:
passengername="pavan",
age="23",
Gender="M",
passengername="kumar",
Gender="M",
Age="22
如果您可以创建参数的模型,则会简洁得多。要从客户端传递它们,需要使用一种数据交换格式对它们进行格式化。我更喜欢使用Newtonsoft提供的JSON。Json库。发送过程由System.Net.Http命名空间提供的HttpClient类处理。以下是一些示例:
服务器端 //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);
}
}
方法如下:
首先,因为你传递了两个对象作为参数,我们需要一个新的类来保存它们(因为我们只能将一个参数绑定到请求的内容):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
属性的值。