application/json 内容类型存储位置

application/json content type store location

内容类型的结果:“application/x-www-form-urlencoded” 存储在 Request.Form 中 Asp MVC.

但是 "application/json" 我找不到商店位置

这是我使用的代码:

ajax部分

    // reading form data
    var form = $("form")[0];
    var formData = new FormData(form);

    var object = {};
    formData.forEach(function(value, key){
      object[key] = value;
    });
    var data = JSON.stringify(object);


    // ajax part
    // POST application/json; charset=utf-8
    $.ajax({
    type: "POST",
    url: "@Url.Action("FormSubmit","Home")",
    data: data,
    dataType: "json",
    async: true,
    contentType: "application/json",
    processData: true,
    success: function(result) { },
    error: function(result) { }
  });

控制器部分

    [HttpPost]
    public ActionResult FormSubmit(string input1, string input2)
    {
        var httpContext= HttpContext;
        var response = Response;
        var request = Request;
        throw new NotImplementedException();
    }

您可以像这样使用控制器:

[HttpPost]
public ActionResult SomeAction(string data)
{
    List<YOUR_MODEL_CLASS> payloadObj = Newtonsoft.Json.JsonConvert.DeserializeObject<List<YOUR_MODEL_CLASS>>(data)(modelData);
    // process your data
}

找到答案 我只需要阅读 post Payload

这是代码

    [HttpPost]
    public ActionResult FormSubmit()
    {
        Request.InputStream.Position = 0;
        var reqMemStream = new MemoryStream(HttpContext.Request.BinaryRead(HttpContext.Request.ContentLength));
        string reqString = Encoding.ASCII.GetString(reqMemStream.ToArray());

        throw new NotImplementedException();
    }