x-www-form-urlencoded传参,body的KEY: Message,VALUE: <OTA_InventoryCheckRQ EchoToken="487ef6b0-3526-45e3-81e7-0eac31754c10" Password="abc888888" PrimaryLangID="en-us" TimeStamp="2021-04-20 10:30:59" UserName="guowang" Version="1.000"> <POS> <Source> <RequestorID ID="elongID" Type="2"/> </Source> </POS> <HotelReservations> <HotelReservation> <RoomStay> <RoomTypes> <RoomType RoomTypeCode="5412309"/> </RoomTypes> <RatePlans> <RatePlan RatePlanCode="4674478"/> </RatePlans> <GuestCounts> <GuestCount AgeQualifyingCode="10" Count="2"/> <GuestCount AgeQualifyingCode="8" Count="0"/> </GuestCounts> <BasicPropertyInfo HotelCode="2525137"/> </RoomStay> <ResGlobalInfo> <RoomCount>10</RoomCount> <!-- 会员登记-开关露出 --> <MemberLevel>V1</MemberLevel> <TimeSpan End="2021-04-30" Start="2021-04-29"/> </ResGlobalInfo> </HotelReservation> </HotelReservations> </OTA_InventoryCheckRQ>,.net core接收的实体模型
时间: 2023-07-19 22:14:35 浏览: 170
form-urlencoded:以x-www-form-urlencoded字符串的形式返回对象
5星 · 资源好评率100%
可以定义一个实体模型来接收 x-www-form-urlencoded 格式的参数。根据你提供的参数,可以定义如下的实体模型:
```
public class InventoryCheckRequest
{
public string EchoToken { get; set; }
public string Password { get; set; }
public string PrimaryLangID { get; set; }
public string TimeStamp { get; set; }
public string UserName { get; set; }
public string Version { get; set; }
public POS POS { get; set; }
public HotelReservations HotelReservations { get; set; }
}
public class POS
{
public Source Source { get; set; }
}
public class Source
{
public RequestorID RequestorID { get; set; }
}
public class RequestorID
{
public string ID { get; set; }
public string Type { get; set; }
}
public class HotelReservations
{
public HotelReservation HotelReservation { get; set; }
}
public class HotelReservation
{
public RoomStay RoomStay { get; set; }
public ResGlobalInfo ResGlobalInfo { get; set; }
}
public class RoomStay
{
public RoomTypes RoomTypes { get; set; }
public RatePlans RatePlans { get; set; }
public GuestCounts GuestCounts { get; set; }
public BasicPropertyInfo BasicPropertyInfo { get; set; }
}
public class RoomTypes
{
public RoomType RoomType { get; set; }
}
public class RoomType
{
public string RoomTypeCode { get; set; }
}
public class RatePlans
{
public RatePlan RatePlan { get; set; }
}
public class RatePlan
{
public string RatePlanCode { get; set; }
}
public class GuestCounts
{
public List<GuestCount> GuestCount { get; set; }
}
public class GuestCount
{
public string AgeQualifyingCode { get; set; }
public int Count { get; set; }
}
public class BasicPropertyInfo
{
public string HotelCode { get; set; }
}
public class ResGlobalInfo
{
public int RoomCount { get; set; }
public string MemberLevel { get; set; }
public TimeSpan TimeSpan { get; set; }
}
public class TimeSpan
{
public DateTime End { get; set; }
public DateTime Start { get; set; }
}
```
在控制器中,可以使用以下代码将 x-www-form-urlencoded 格式的参数转换成实体模型:
```
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Xml.Serialization;
using Microsoft.AspNetCore.Mvc;
public class TestController : Controller
{
[HttpPost]
public IActionResult ReceiveMessage()
{
using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
{
string bodyStr = reader.ReadToEnd();
string message = WebUtility.UrlDecode(bodyStr.Substring(8));
// 将 message 转换成实体模型
XmlSerializer serializer = new XmlSerializer(typeof(InventoryCheckRequest));
using (StringReader stringReader = new StringReader(message))
{
InventoryCheckRequest request = (InventoryCheckRequest)serializer.Deserialize(stringReader);
// 对实体模型进行处理
}
return Ok();
}
}
}
```
在上面的代码中,使用了 XmlSerializer 来将 XML 格式的字符串转换成实体模型。需要注意的是,实体模型中的 DateTime 类型需要在 XML 中使用 ISO 8601 格式表示。
阅读全文