首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将JSON发送到ASP.NET MVC5?(非网络API)

将JSON发送到ASP.NET MVC5?(非网络API)
EN

Stack Overflow用户
提问于 2017-05-26 13:50:23
回答 1查看 221关注 0票数 0

有办法将JSON数据发布到MVC5操作吗?而不是WebAPI!

问题是:我的操作会被调用,但是传入模型的所有属性都是空的。

型号:

代码语言:javascript
复制
using Newtonsoft.Json;

[JsonObject()]
[Serializable()]
public class DemoModel
{
    [JsonProperty()]
    public string a { get; set; }

    [JsonProperty()]
    public string b { get; set; }
}

行动是:

代码语言:javascript
复制
[HttpPost()]
public ActionResult demoaction(DemoModel model)
{
    //model.a here is null, should be "AAA"
    //model.b here is null, should be "BBB"
}

我一直在用Fiddler来模拟请求。我的帖子请求如下:

代码语言:javascript
复制
POST http://localhost:9000/demoaction HTTP/1.1
Host: localhost:9000
Connection: keep-alive
Accept: */*
Content-Type: application/json
Content-Length: 60

身体看起来是这样的:

代码语言:javascript
复制
{"a":"AAA","b":"BBB"}

所以,再问一次:我如何将JSON发布到一个操作中?在我的例子中,模型的所有属性都是空的。

EN

Stack Overflow用户

发布于 2017-06-01 21:34:40

我只是使用这些代码,它在MVC5操作上工作得很好,但是要确保在POST请求头中使用的是Content-Type : application/json,而不知道它不能正确工作,而且模型仍然是空的。

代码语言:javascript
复制
public class DemoModel
    {

        public string a { get; set; }


        public string b { get; set; }
    }

[HttpPost]
        public ActionResult Index(DemoModel model)
        {

            return View();
        }
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44203327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档