Как передать больше одного параметра в контроллер?

У меня есть действие в контроллере:

public void CreateArticle([FromBody]string header, [FromBody]string content)
{
  // something code
}

И fetch-запрос:

fetch(
"/article", {
  method: "POST",
  body: JSON.stringify({
    header: "Hello World!",
    content: 'Console.WriteLine("Hello World!")'
  })
});

При запуске проекта возникает ошибка:

InvalidOperationException: Action 'ArticleController.CreateArticle()' has more than one parameter that was specified or inferred as bound from request body. Only one parameter per action may be bound from body. Inspect the following parameters, and use 'FromQueryAttribute' to specify bound from query, 'FromRouteAttribute' to specify bound from route, and 'FromBodyAttribute' for parameters to be bound from body: string header, string content

Как передавать в действие контроллера больше одного параметра через тело fetch-запроса?


Ответы (1 шт):

Автор решения: Exploding Kitten

Например, так:

public class Article
{
    public string Header { get; set; }
    public string Content { get; set; }
}

// ...

[HttpPost]
public void CreateArticle([FromBody] Article article)
{
    // ...
}
fetch(
    "/article", {
    method: "POST",
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        header: "Hello World!",
        content: 'Console.WriteLine("Hello World!")'
    })
});
→ Ссылка