Post received by FromBody causes serializable error

后端 未结 2 1653
北荒
北荒 2021-01-12 18:29

Here\'s the basic setup, I have an asp.net core webapi controller (in c#) with a post function like so:

[HttpPost]
public ActionResult Post([Fr         


        
2条回答
  •  离开以前
    2021-01-12 19:10

    The serializable error is actually a parsing error from JSON.NET, but the problem actually has nothing to do with parsing JSON.

    The real issue is that ASP.NET Core expects to parse a JSON body into an object/DTO. So you have two options you can use to fix the problem:

    1. Make a simple DTO container object for your single parameter e.g.:

      public class SimpleObject { 
          public string Name { get; set; } 
      }
      
    2. Instead of passing a full fledged JSON object in your request body, just use a simple string e.g: "My parameter string"

提交回复
热议问题