简体   繁体   中英

Blazor Error 400,ReasonPhrase Bad Request Version: 1.1 System.Net.Http.BrowserHttpHandler

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
}

Best guess:

AppFolder has a required string Name property and you leave it null .

This can easily happen when nullable reference types is ON.

如果你想使用 [POST]、[PUT] 和 [DELETE] 你需要用这些属性来装饰你的控制器[HttpPost] [HttpPut] [HttpDelete]

Thank you for all recommend. I has fixed this problem. I have any property null I think virtual type will be not effect with model then in not mind it.But it's the problem.

this is my issue.

 public class AppFolder
{
    [Key]
    public int Id { get; set; }
    public string FoldName { get; set; }
    public string FolderColor { get; set; }
    public virtual ICollection<AppFile> AppFiles { get; set; }
    public bool IsDelete { get; set; }
    public string CreatorId { get; set; }
    public DateTime CreatedDate { get; set; }
    public DateTime UpdatedDate { get; set; }
}

And i fixed it by allow null for AppFiles.

public class AppFolder
{
    [Key]
    public int Id { get; set; }
    public string FoldName { get; set; }
    public string FolderColor { get; set; }
    public virtual ICollection<AppFile>? AppFiles { get; set; }
    public bool IsDelete { get; set; }
    public string CreatorId { get; set; }
    public DateTime CreatedDate { get; set; }
    public DateTime UpdatedDate { get; set; }
 }

Note: Reference types will need to be made nullable if it is possible for it to be null. This needs to be done even if the field has [JsonIgnore] This took me 3 days to find out... hope it helps you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM