【问题标题】:Web Api does not receive postsWeb Api 收不到帖子
【发布时间】:2020-03-28 02:17:44
【问题描述】:

我已经度过了一天的大部分时间,我的老师也无法解决这个问题..

我有一个 ac# Web Api 2 项目,我正在尝试使用“邮递员”在我的机器上本地发布到项目.. 可以正常工作,但只有在我不传递任何参数时才能发布...

我的 SystemsController 中的操作

[HttpPost]
        public IHttpActionResult Post([FromBody] SystemModel data)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest("The request was not formatted correctly");
            }

        using (ModelContainer entities = new ModelContainer())
        {
            entities.Systems.Add(new Systems()
            {
                Name = data.Name,
                Description = data.Description,
                Longtitude = data.Longtitude,
                Latitude = data.Latitude,
                Brometer = data.Brometer,
                Imagepath = data.Imagepath,
            });

            entities.SaveChanges();

            return Ok();
        }
    }

我的模特:

public class SystemModel
    {
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        public string Description { get; set; }
        public double Longtitude { get; set; }
        public double Latitude { get; set; }
        public double Brometer { get; set; }
        public string Imagepath { get; set; }
    } 

Postman 中的 Json:

{
    "Name":"Dette er en test", 
    "Description": "en beskrivelse",
    "Longtitude": "31.0200",
    "Latitude": "-121.00",
    "Brometer": "1025.31",
    "ImagePath": "somepath/to/image.jpg"
}

无论我做什么,我都不会发布帖子。

我发帖的网址是 localhost:44389/api/Systems

我不知所措.. 到目前为止,谷歌搜索并没有带来任何解决方案.. 据我所见,我做得正确..还是?

更新 所以我尝试将 IHttpActionResult 更改为 SystemModel,然后只返回数据......现在它可以工作了......为什么,我不知道!?!

所以我现在看起来像这样……

 [HttpPost]
        public SystemModel Post(SystemModel data)
        {
            if (!ModelState.IsValid)
            {
                return null;
            }

            using (ModelContainer entities = new ModelContainer())
            {
                var record = new Systems()
                {
                    Name = data.Name,
                    Description = data.Description,
                    Longtitude = data.Longtitude,
                    Latitude = data.Latitude,
                    Brometer = data.Brometer,
                    Imagepath = data.Imagepath,
                };
                entities.Systems.Add(record);

                entities.SaveChanges();

                return new SystemModel() // returns the inserted object
                {
                    Id = record.Id,
                    Name = record.Name,
                    Description = record.Description,
                    Longtitude = record.Longtitude ?? default(double),
                    Latitude = record.Latitude ?? default(double),
                    Brometer = record.Brometer ?? default(double),
                    Imagepath = record.Imagepath,
                };
            }
        }

【问题讨论】:

  • 尝试更改您的 json,以便所有属性名都以小写字母开头。您也不需要参数中的 [FromBody] 部分
  • 尝试改变它但没有区别
  • 另外,Imagepath 在你的 Json 中有一个大写的 P,也要改变它。你在邮递员中得到什么回应?
  • 我设置了imagepath,又试了一次,但没有什么不同,postman一直在运行,它什么也没说,所以我每次都要取消它..
  • 它是否在控制器中遇到断点?

标签: c# post asp.net-web-api2 postman


【解决方案1】:

在标头中将Content-Type 设置为application/json

邮递员中的json是

{
"Name":"Dette er en test", 
"Description": "en beskrivelse",
"Longtitude": 31.0200,
"Latitude": -121.00,
"Brometer": 1025.31,
"ImagePath": "somepath/to/image.jpg"
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 2022-12-21
    • 2015-06-20
    • 2013-12-03
    • 2020-04-11
    相关资源
    最近更新 更多