C# 如何在webapi中将参数传递给数组类?
我是asp.net mvc webapi新手。我正在创建一个webapi服务。在此服务中,我将以数组类的形式发送参数 以下是我的服务:C# 如何在webapi中将参数传递给数组类?,c#,asp.net-mvc,asp.net-web-api2,C#,Asp.net Mvc,Asp.net Web Api2,我是asp.net mvc webapi新手。我正在创建一个webapi服务。在此服务中,我将以数组类的形式发送参数 以下是我的服务: [AcceptVerbs("GET", "POST")] public HttpResponseMessage addBusOrder(string UserUniqueID, int PlatFormID, string DeviceID, int RouteScheduleId,
[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; }
}
现在,如何将乘客
和联系人信息
参数传递给上述服务
webapiconfig文件中是否有任何更改?
我想传递如下乘客详细信息:
passengername="pavan",
age="23",
Gender="M",
passengername="kumar",
Gender="M",
Age="22
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 */
}
}
如果你能为你的参数建立一个模型,它会更整洁。要从客户端传递它们,需要使用数据交换格式之一对它们进行格式化。我更喜欢使用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);
}
}
客户端
//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);
}
}
以下是您如何做到这一点: 首先,由于要将两个对象作为参数传递,我们需要一个新类来保存它们(因为我们只能将一个参数绑定到请求的内容): 现在,对于控制器(这只是一个测试控制器): 您的请求应该如下所示:
passengername="pavan",
age="23",
Gender="M",
passengername="kumar",
Gender="M",
Age="22
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 */
}
}
POST http://@Webruster请查看我的密码,并帮助我。请任何人帮助我。我在过去的两天里一直在尝试,但我仍然没有得到回答。passengerName
是否确认了乘客身份?我的意思是,每个乘客都有唯一的名字吗?@jhmt是的,每个乘客都有自己的名字name@jhmt我想这样通过考试:passengername=“pavan”,Age=“23”,Gender=“M”,passengername=“kumar”,Age=“22”,Gender=“M”有时我想发送更多的乘客。那么如何动态发送乘客数据乘客属性是一个数组,因此您可以在Json数组中传递任意数量的乘客