I created an Asp Net Razor project in visual studio 2019.
I'm learning the technology with some exercises. Specifically, I would like to display on a page a chart whose data i would like to retrieve via an ApiController.
[Route("api/[controller]")]
[ApiController]
public class AjaxAPIController : ControllerBase
{
public ChartData chartData = new ChartData();
[HttpGet]
[Route("ChartDataJson")]
public IActionResult ChartDataJson()
{
// First example
var x = new List<double> { 1.0,};
var y = new List<double> { 10.0,};
var chartData = new { x, y };
return Ok(chartData);
}
[HttpGet]
[Route("ChartDataJson2")]
public IActionResult ChartDataJson2()
{
// Second Example
var x = new List<double> {1.0, 20.0, 30.0 };
var y = new List<double> {2.0, 30.0, 45.0};
var chartData = new { x, y };
return Ok(chartData);
}
[HttpPost]
[Route("UpdateChartData")]
public IActionResult UpdateChartData([FromBody] ChartData data)
{
// Read received data
var newDataX = data.X;
var newDataY = data.Y;
var chartData = new { x = new List<double>[] { newDataX }, y = new List<double>[] { newDataY } };
return Ok(chartData);
}
}
The values of the chart, can be updated in 3 different ways:
- Click button 1: update the values of the chart with fixed values; (Get)
// When the button 1 is clicked, get data from the controller and update the chart
$("#btnGet").click(function () {
console.log("button 1 click is executed.");
$.get("/api/AjaxAPI/ChartDataJson", function (data) {
updateScatterChart(data);
});
});
- Click button 2: same as point 1 but with different values; (Get)
// When the button 2 is clicked, get data from the controller and update the chart
$("#btnGet2").click(function () {
console.log("button 2 click is executed.");
$.get("/api/AjaxAPI/ChartDataJson2", function (data) {
updateScatterChart(data);
});
});
- Call post: when I receive from outside a post call with some data in json format. (Post)
// Update the data chart when a post request is received
$.post("/api/AjaxAPI/UpdateChartData", function (data) {
console.log("X values:", data.x);
console.log("Y values:", data.y);
updateScatterChart(data);
});
The result is something like:
Steps 1 and 2 work correctly but step 3 doesn't work...
The first problem occurs when the page is loaded:
jquery.min.js:2 POST https ://localhost:44354/api/AjaxAPI/UpdateChartData 400 (Bad Request)
In addition, if I try to launch in the browser console the script:
fetch("https://localhost:44354/api/AjaxAPI/UpdateChartData", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ X: [400.0], Y: [600.0] }),
})
.then((response) => response.json())
.then((data) => console.log(data))
.catch((error) => console.error("Error: " + error));
to simulate the post request, I can in debug read the values in the ApiController class but I can't correctly update the values of the chart.
My full code is available here: https://github.com/DanielScerpa/RazorPageChart/tree/dev_chart
Summarizing:
I would like to understand how I can correctly update my chart values when I receive a post request and I would like to understand the startup problem 400 (Bad Request)
!!!...EDIT...!!!
Summarazing:
- How can I automatically update the chart when I receive a post call in the apicontroller?
Example, someone calls:
<https://localhost:44354/api/AjaxAPI/UpdateChartData>
I intercept the post call via the code:
[HttpPost]
[Route("UpdateChartData")]
public IActionResult UpdateChartData([FromBody] ChartData data)
{
// Read received data
var newDataX = data.X;
var newDataY = data.Y;
var chartData = new { x = new List<double>[] { newDataX }, y = new List<double>[] { newDataY } };
return Ok(chartData);
}
}
at this point how can I update the chart with the newly retrieved values (data.x and data.y)?
- How can I solve 404 bad request error at startup?

From your requirement, you are expecting the web application to be updated for the chart when any user posts the request to the "UpdateChartData" API.
This requires the SignalR for the real-time communication. The full demo for the installation and tutorial you may refer to the link provided.
As you are using the .NET Core 2.2 for the project and this version is deprecated, the only supported version for Microsoft.AspNetCore.SignalR.Common is with version 1.1.0. The provided steps below are for setting up the SignalR in your application specified for .NET Core 2.2.
Via NuGet, install Microsoft.AspNetCore.SignalR.Common 1.1.0
Right-click the project > Manage Client-side Libraries, it will generate a libman.json file. In the file, you install the @microsoft/signalr library as below:
ChartHubfrom the DI. And emit the data to all clients.