JSON如何设计EF模型并反序列化到其中

时间:2019-03-16 15:22:33

标签: asp.net-web-api json.net entity-framework-6

我正在尝试实现asp.net Web API 2 Rest服务。我正在尝试根据其他rest API的响应来设计模型,并使用EF将值保存到数据库中。

这是示例响应JSON:

"{\"referenceId\":\"0000000029\",\"paymentId\":\"MDO1036616\",\"productCode\":\"001002461285\",\"quantity\":\"1\",\"currency\":\"TL\",\"unitPrice\":\"46,47\",\"totalPrice\":\"46,47\",\"merchantProductCode\":\"\",\"purchaseStatusCode\":\"00\",\"purchaseStatusDate\":\"2019-03-16T14:46:00Z\",\"coupons\":[{\"serials\":[\"100b7fe6-581d-46aa-aee7-8429b51edd20\"],\"pins\":[\"PL46-WR9T-FTGD4-3PXC-9MCSa\"]}],\"version\":\"V1\",\"signature\":\"aee9dda307fa5001494c0fe793c1aa20\",\"applicationCode\":\"52e7cf966b724749a7c4efadc3727ed7\"}"

这是我的模特:

public class ConfirmResponse
{
    public int Id { get; set; }
    public string referenceId { get; set; }
    public string version { get; set; }
    public string signature { get; set; }
    public string paymentID { get; set; }
    public string productCode { get; set; }
    public string currency { get; set; }
    public string ApplicationCode { get; set; }
    public float unitPrice { get; set; }
    public float totalPrice { get; set; }
    public string purchaseStatusCode { get; set; }
    public DateTime? purchaseStatusDate { get; set; }
    public DateTime? requestDateTime { get; set; } = DateTime.Now;
    public string merchantProductCode { get; set; }
    public Coupon[] coupons { get; set; }


}

我不知道如何在JSON中为优惠券创建模型。

这是我的控制人:

[HttpPost, Route("confirmation")]
    public async Task<IHttpActionResult> PostConfirmation(ConfirmRequest confirm)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }


        var applicationCode = System.Configuration.ConfigurationManager.AppSettings["ApplicationCode"];
        var secretKey = System.Configuration.ConfigurationManager.AppSettings["SecretKey"];
        var version = System.Configuration.ConfigurationManager.AppSettings["Version"];
        var source = applicationCode + confirm.referenceId + version + confirm.validatedToken+ secretKey;

        using (var md5Hash = MD5.Create())
        {
            var hash = GetMd5Hash(md5Hash, source);
            confirm.signature = hash;
            confirm.ApplicationCode = applicationCode;
            confirm.version = version;

        }

        context.ConfirmRequests.Add(confirm);
        await context.SaveChangesAsync();

        HttpClient httpClient = new HttpClient();


        HttpContent content = new StringContent(
            JsonConvert.SerializeObject(confirm),
            Encoding.UTF8,
            "application/json"
        );

        var response =
            await httpClient.PostAsync("https://test.com/purchaseconfirmation", content);

       (response.ToString());
        var htmlResponse = string.Empty;
        if (response != null)
        {
            switch (response.StatusCode)
            {
                case HttpStatusCode.NotFound:
                    return NotFound();

                case HttpStatusCode.InternalServerError:
                    return InternalServerError();

                case HttpStatusCode.OK:
                    htmlResponse = await response.Content.ReadAsStringAsync();
                    //Adding Response into database
                    context.ConfirmResponses.Add(JsonConvert.DeserializeObject<ConfirmResponse>(htmlResponse));
                    await context.SaveChangesAsync();
                    return Ok(htmlResponse);

                case HttpStatusCode.BadRequest:
                    return BadRequest();

                case HttpStatusCode.Unauthorized:
                    return Unauthorized();

                case HttpStatusCode.RequestTimeout:
                    return InternalServerError();

                default:
                    htmlResponse = await response.Content.ReadAsStringAsync();
                    break;
            }
        }

        return Ok(htmlResponse);
    }

我想我需要创建/设计一个或多个模型以存储序列号和引脚。如果是这样,我是否需要在控制器中使用事务?我应该如何更改SaveChangesAsync部分?

最好的问候。

0 个答案:

没有答案