我已经在经典的web apis中做了好几次,但在Azure Functions中没有,所以我不知道我在这里缺少什么。
我的实体 "用户":
[SharedCosmosCollection("shared")]
public class User : ISharedCosmosEntity
{
/// <summary>
/// User id
/// </summary>
[JsonProperty("Id")]
public string Id { get; set; }
/// <summary>
/// Cosmos entity name for shared collection
/// </summary>
[CosmosPartitionKey]
public string CosmosEntityName { get; set; }
public string GivenName { get; set; }
public string FamilyName { get; set; }
public string NickName { get; set; }
public string Name { get; set; }
public string Picture { get; set; }
public string Locale { get; set; }
public DateTime UodatedAt { get; set; }
public string Email { get; set; }
public bool EmailVerified { get; set; }
public string Sub { get; set; }
}
我的函数代码为CreateUser:
[FunctionName("CreateUser")]
public static async Task<IActionResult> CreateUser(
[HttpTrigger(AuthorizationLevel.Function,
"post", Route = "user")]
HttpRequest req)
{
var telemetry = new TelemetryClient();
try
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var input = JsonConvert.DeserializeObject<User>(requestBody);
var userStore = CosmosStoreHolder.Instance.CosmosStoreUsers;
var added = await userStore.AddAsync(input);
return new OkObjectResult(added);
}
catch (Exception ex)
{
string guid = Guid.NewGuid().ToString();
var dt = new Dictionary<string, string>
{
{ "Error Lulo: ", guid }
};
telemetry.TrackException(ex, dt);
return new BadRequestResult();
}
}
而在门户中,我在请求体中发送了这个JSON
{
"given_name": "aaa",
"family_name": "bbb",
"nickname": "xx.xx.psg",
"name": "bbbb",
"picture": "https://lh3.googleusercontent.com/a-/AOhsGg8qaBLDPubSaNb3u8zMyiUGrwFE3zhQ8MMqLALjGc",
"locale": "es",
"updated_at": "2020-04-20T15:33:16.133Z",
"email": "[email protected]",
"email_verified": true,
"sub": "google-oauth2|1111"
}
但是,我得到了一个http 500错误,不知道是什么问题。
如果类属性与JSON属性不完全匹配,序列化器就无法将JSON属性与类属性匹配,没有一点帮助。 要将JSON属性映射到类属性,可以使用 JsonProperty
属性。这将适用于序列化和反序列化。
[JsonProperty("given_name")]
public string GivenName { get; set; }