我在Visual Studio中使用Blazor WebAssembly App Template创建了一个新的项目/解决方案.
然后将此REST Api方法添加到现有的WeatherForecastControl类中:
[HttpPost]
public async Task<ActionResult> Post(string str)
{
return Ok();
}
然后我试着从Blazor页面调用它:
@inject HttpClient Http
...
private WeatherForecast[]? forecasts;
protected override async Task OnInitializedAsync()
{
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast"); // this works
var reply = await Http.PostAsJsonAsync("WeatherForecast", "test the west"); // this returns 400 Bad Request
}
当try 使用HTTPPut时,同样的错误请求也会发生.
我首先try 了我自己的应用程序(它也是基于相同的模板构建的). 也try 了使用HttpClient.PostAsync.这里也有同样的错误.