{ "FData": { "UserName": "test", "Mail": "test@gmail.com.tw", "IDN": "N123456789" }, "FInfo": { "SN": "3274", "Message": "1544", "FormNumber": "001", "Applicant": "test", "AppDate": "2020-11-11 11:52:21", "AppUnit": "Manager" }, "Approve": "同意", "Reject": "不同意" } public class FData { public string UserName { get; set; } public string Mail { get; set; } public string IDN { get; set; } } public class FInfo { public string SN { get; set; } public string Message { get; set; } public string FormNumber { get; set; } public string Applicant { get; set; } public string AppDate { get; set; } public string AppUnit { get; set; } } public class Root { public FData Data { get; set; } public FInfo Info { get; set; } public string Approve { get; set; } public string Reject { get; set; } } using Microsoft.AspNetCore.Mvc; using EAudit.Models; using Microsoft.EntityFrameworkCore; using System.Threading.Tasks; using System.Collections.Generic; using System.Linq; namespace EAudit.Controllers { [Route(“v1/[controller]”)] [ApiController] public class FlowTestController : ControllerBase { private readonly AuditContext _context; public FlowTestController(AuditContext context) { _context = context; } Controller就可以這樣設計 [HttpPost] public async Task> Post1(Root jsonResult) { _context.TestViewModels.Add(new TestFlow.TestViewModel { UserName = jsonResult.FormData.UserName, Mail = jsonResult.FormData.Mail, IDN = jsonResult.FormData.IDN, SN = jsonResult.FormInfo.SN, SMS = jsonResult.FormInfo.SMS, FormNo = jsonResult.FormInfo.FormNo, Applicant = jsonResult.FormInfo.Applicant, ApplicantDate = jsonResult.FormInfo.ApplicantDate, ApplicantUnit = jsonResult.FormInfo.ApplicantUnit, ApproveAPI = System.Web.HttpUtility.UrlDecode(jsonResult.ApproveAPI), RejectAPI = System.Web.HttpUtility.UrlDecode(jsonResult.RejectAPI) }); await _context.SaveChangesAsync(); return Ok("OK"); } // 舊版功能 // [HttpPost] // public async Task > Post1(TestFlow.TestViewModel testViewModels) // { // _context.TestViewModels.Add(testViewModels); // await _context.SaveChangesAsync(); // return Ok("Successful"); // } [HttpGet] public async Task >> Get1() { // return await _context.AuditHeaderAlls.ToListAsync(); // return await _context.TestViewModels.ToListAsync(); int maxId = _context.TestViewModels.MaxAsync(X => X.Id).Id; return await _context.TestViewModels.Where(x => x.Id == maxId).ToListAsync(); } public class FormData { public string UserName { get; set; } public string Mail { get; set; } public string IDN { get; set; } } public class FormInfo { public string SN { get; set; } public string SMS { get; set; } public string FormNo { get; set; } public string Applicant { get; set; } public string ApplicantDate { get; set; } public string ApplicantUnit { get; set; } } public class Root { public FormData FormData { get; set; } public FormInfo FormInfo { get; set; } public string ApproveAPI { get; set; } public string RejectAPI { get; set; } } } }
2020-11-29
[C#]Json轉換Class
https://json2csharp.com/
以下是Json格式可以轉成Class
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言