Backend, ASP.net Core API:
[Produces(\"application/json\")]
[Route(\"api/[controller]\")]
public class StoriesController : Controller
{
p
Since you're passing your value as JSON (as the screenshot suggests), you should use the model binding correctly and use a proper class instead of a string input:
public class StoryAddRequest
{
public string Value { get; set; }
}
You then can use it in your controller:
// POST api/values
[HttpPost]
public void Post([FromBody] StoryAddRequest request)
{
if (request != null)
{
Story story = new Story
{
content = request.Value,
timeOfAdding = DateTime.Now,
numberOfViews = 0
};
STORIES.Add(story);
}
}
From the documentation:
Request data can come in a variety of formats including JSON, XML and many others. When you use the
[FromBody]
attribute to indicate that you want to bind a parameter to data in the request body, MVC uses a configured set of formatters to handle the request data based on its content type. By default MVC includes aJsonInputFormatter
class for handling JSON data, but you can add additional formatters for handling XML and other custom formats.