【问题标题】:Posting to asp.net core mvc app results in null body and null input parameter发布到 asp.net core mvc 应用程序导致空正文和空输入参数
【发布时间】:2016-12-07 17:06:48
【问题描述】:

这是我的第一个 asp.net 核心应用程序,所以我可能遗漏了一些明显的东西,但我查看了其他 StackOverflow 答案,那里的解决方案对我没有帮助。我有一个在服务结构上运行的 asp.net 核心 mvc 应用程序,我正在尝试将字符串发布到控制器。该字符串原本是一个json对象数组。

我的 ajax 帖子:

var sendThis = { "operations": JSON.stringify(operations) };
$.ajax({
    url: '/Home/Execute',
    type: 'POST',
    data: sendThis,
    dataType: "json",
    contentType: 'application/json; charset=utf-8',
    error: function (xhr) {
        $("#save-footer-text").val("Saving failed. Please contact an administrator");
    },
    success: function (result) {
        $(".save-footer").addClass("slider");
    },
    async: true
});

我的控制器在另一边。我从另一个堆栈溢出答案中获取了流的东西,但它在完成后只返回一个空字符串。

[HttpPost]
public IActionResult Execute([FromBody] string operations /*this is null*/)
{
    var i = 5;
    string documentContents; //this will be an empty string.
    Request.Body.Position = 0;
    using (Stream receiveStream = Request.Body)
    {
        using (StreamReader readStream = new StreamReader(receiveStream, Encoding.Unicode))
        {
            documentContents = readStream.ReadToEnd();
        }
    }
    Console.WriteLine(i);
    return new OkResult();
}

从其他 stackoverflow 答案中,我也尝试过将传统设置为 true 进行发布,并且尝试将操作发布到模型中,就像这样

public class M
{
    public string status { get; set; }
    public string appName { get; set; }
    public string startTime { get; set; }
    public string endTime { get; set; }
    public string description { get; set; }
    public string operation { get; set; }
}

将控制器更改为公共IActionResult Execute([FromBody] List<M> operations)

我检查了我的 javascript 确实发送了一个请求,Chrome 工具报告请求有效负载是:

operations= 我的 json 字符串在这里

我在 Fiddler 中也看到了这一点,所以我知道它正在走线。

根据this 的回答,我也尝试过更新 JsonSettings,但这也没有帮助。

我将 1.0.0-rc2-final 用于 asp.net 核心包,将 5.1.150 用于服务结构。

我在这里缺少什么?对不起,如果答案真的很琐碎。感谢您的帮助。

【问题讨论】:

  • 您没有发回响应中的内容。 return Ok(documentContents);
  • 控制器中为空字符串。我的问题是我收到的数据是空的。
  • 在服务结构中运行之前,您的应用程序是否在本地运行?
  • 不确定。 Visual Studio 尝试进入调试模式并且只是冻结。我还没有尝试过创建一个没有服务结构的核心应用程序。我会调查的。我应该指出,所有的获取和应用程序的其余部分都可以工作。
  • 如果删除[FromBody] 属性会发生什么?

标签: c# json asp.net-core asp.net-core-mvc azure-service-fabric


【解决方案1】:

如果脚本中的操作变量是一个数组

    var operations = new Array();

    var op1 = { status: 's1', appName: 'a1', startTime: 'st1', endTime: 'e1', description: 'd1', operation: 'o1' };
    operations.push(op1);

    var op2 = { status: 's2', appName: 'a2', startTime: 'st2', endTime: 'e2', description: 'd21', operation: 'o2' };
    operations.push(op2);

    var sendThis = JSON.stringify(operations);
    ...// ajax call here

并且控制器 post 方法定义如下,然后List<M> operations 应该包含预期通过线路发送的值。

    [HttpPost]
    public IActionResult Execute([FromBody]List<M> operations)
    {
        // ... some code here
        return Json("");
    }

【讨论】:

    【解决方案2】:

    这是一个 M 的列表?

    如果是,那么你的json不应该是这样的

    var sendThis = { "operations": JSON.stringify(operations) }; 
    

    试试这个:

    var sendThis = JSON.stringify(operations);
    

    我认为 asp.net 正在尝试反序列化这样的对象:

    public class Operation
    {
        public List<M> Operations { get; set; }
    }
    

    【讨论】:

    • 我在使用模型绑定方法时尝试过这个。我也尝试过不使用 stringify(并传入实际对象),但两种方法都没有奏效。
    猜你喜欢
    • 2020-06-21
    • 2017-02-03
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多