简体   繁体   中英

Error parsing HTTP POST request body in my web api endpoint

I have a Web API project with a POST endpoint. I'm trying to hit this endpoint with a request with a JSON body and have it be translated into the parameter of my function for that endpoint. However, the parameter in my function is always all null.

Post function endpoint:

[HttpPost]
[Route("PostMedia/")]
public void UpdateMedia([FromBody] Media value)
{
    string[] file = Directory.GetFiles(this.config.Value.JSONFileDirectory, value.Id.ToString() + ".json");
    if (file.Length == 1)   
    {
        try
        {
            using (StreamReader reader = new StreamReader(file[0]))
            {
                string json = reader.ReadToEnd();
            }
        }
        catch (Exception e)
        {
            throw new Exception("Could not parse file JSON for ID: " + value.Id.ToString(), e);
        }
    }
}

My Media model class & its CatalogBase parent:

public class Media : CatalogueBase
{
    MediaType type;
    MediaRating rating;
    string genre;

    public MediaType Type { get => type; set => type = value; }
    public MediaRating Rating { get => rating; set => rating = value; }
    public string Genre { get => genre; set => genre = value; }
}

public abstract class CatalogueBase
{
    string name;
    string description;
    int id;

    public string Name { get => name; set => name = value; }
    public string Description { get => description; set => description = value; }
    public int Id { get => id; set => id = value; }
}

JSON Request I'm hitting my API with:

{
    "Media" : {
        "Id": 1,
        "Name": "Gettysburg",
        "Description": "A movie set during the American Civil War",
        "Type": "Movie",
        "Rating": "Excellent",
        "Genre" : "Drama"
    }
}

What's happening is that I'm hitting my endpoint, but the (Media value) parameter is always a null/default value. It doesn't actually populate anything with the data from the body of the POST request I'm hitting it with from postman. Any idea why my model class is not being populated by the framework?

This is what model parameter looks like in the debugger:

调试器中的模型参数

The model binder is unable to map the incoming JSON to the class definition.

Either remove the root object from the JSON to match the object model

{ 
    "Id": 1,
    "Name": "Gettysburg",
    "Description": "A movie set during the American Civil War",
    "Type": "Movie",
    "Rating": "Excellent",
    "Genre" : "Drama" 
}

Or update the desired model to match the sent JSON.

public class MediaUpdateModel {
    public Media Media { get; set; }
}

and use that for the action

public void UpdateMedia([FromBody] MediaUpdateModel value) {
    var media = value.Media;

    //...
}

Reference Model Binding in ASP.NET Core

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM