Please suggest me. I initial project with Dotnet FrameworkCore6. Create project with Hosted and Individual. I try to use HTTPClient.Get it's work but i use HTTPClient.Put,Post,Delete,Patch i got Error 400. How can i fix it. Now i having 2 project get the same problem.
In blazor page
public async Task OnSubmit(AppFolder args)
{
args.IsDelete = false;
args.FolderColor = "";
args.CreatorId = "";
var httpClient = _factory.CreateClient("public");
var result = await httpClient.PostAsJsonAsync("WeatherForecast",args);
Console.WriteLine("OnSubmit Debug : " result);
}
In WeatherController
[HttpPost]
public async Task<ActionResult> PostSomethings(AppFolder args)
{
Console.WriteLine("Post Debug" args);
return await Task.Run(() => Ok());
}
I has bypass authen Program.cs
builder.Services.AddHttpClient("public", client => client.BaseAddress = new
Uri(builder.HostEnvironment.BaseAddress));
I got this Error Message
OnSubmit Debug : StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content:
System.Net.Http.BrowserHttpHandler BrowserHttpContent, Headers:
{
Date: Sat, 18 Jun 2022 11:58:57 GMT
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET
Content-Type: application/problem json; charset=utf-8
}
CodePudding user response:
If you want to use [POST], [PUT] and [DELETE] you need to decorate your controllers with those attributes [HttpPost] [HttpPut] [HttpDelete]
CodePudding user response:
Best guess:
AppFolder has a required string Name
property and you leave it null
.
This can easily happen when nullable reference types is ON.
CodePudding user response:
The issue is with the PostAsJsonAsync
method... You must tell the framework what is the type of the argument args
to serialize. The type of args
is AppFolder
, and your code should look something like this:
await httpClient.PostAsJsonAsync<AppFolder>("WeatherForecast",args);