【发布时间】:2019-12-10 20:28:45
【问题描述】:
我试图在 ASP.net WebAPI 中读取表单 url 编码的正文。 如果表单变量和我的模型变量名称相同(不区分大小写),则映射是直接的,但如果名称不同,则映射失败。
例如,
POST /api/values HTTP/1.1
Host: localhost:62798
Accept: text/json
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
Postman-Token: 51ee1c5f-acbb-335b-35d9-d2b8e62abc74
uid=200&email=john%40jones.com&first_name=John&last_name=jones&phone=433-394-3324&city=Seattle&state_code=WA&zip=98105
public class SampleModel{
public string UID { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string City { get; set; }
public string StateCode { get; set; }
public string Zip { get; set; }
}
注意名字、姓氏和状态码变量。如果模式有first_name,它将被正确映射。但是如果你想遵守 C# 命名约定,我们需要将first_name 映射到FirstName。
我知道如果它是 json,我可以很容易地将它映射到 JSON 属性名称,但是如何使用 urlencoded 表单数据来完成呢?
【问题讨论】:
标签: c# asp.net-web-api x-www-form-urlencoded