简体   繁体   English

Blazor 错误 400,ReasonPhrase 错误请求版本:1.1 System.Net.Http.BrowserHttpHandler

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

Please suggest me.请给我建议。 I initial project with Dotnet FrameworkCore6.我使用 Dotnet FrameworkCore6 进行了初始项目。 Create project with Hosted and Individual .使用HostedIndividual创建项目。 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.我尝试使用HTTPClient.Get它的工作,但我使用HTTPClient.Put,Post,Delete,Patch我得到错误 400。我该如何解决它。 Now i having 2 project get the same problem.现在我有 2 个项目遇到了同样的问题。

In blazor page在 Blazor 页面中

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我已经绕过 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 . AppFolder 具有必需的string Name属性,您将其保留为null

This can easily happen when nullable reference types is ON.当可空引用类型为 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.我有任何属性null我认为virtual类型不会对模型产生影响然后不要介意它。但这是问题所在。

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.我通过允许 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!即使该字段有 [JsonIgnore] 也需要这样做这花了我 3 天的时间才发现......希望它对你有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 状态码 400 原因短语“错误请求”版本 1.1 - statuscode 400 reasonphrase 'bad request' version 1.1 状态代码 500,ReasonPhrase:&#39;内部服务器错误&#39;,版本:1.1,内容:System.Net.Http.StreamContent - Status code 500, ReasonPhrase:'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent 获取 StatusCode:401,ReasonPhrase:&#39;Unauthorized&#39;,版本:1.1,内容:System.Net.Http.StreamContent,通过代码调用 API 时出现标题错误 - Getting StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers error when Calling a API via code System.Net.Http.HttpRequestException:400(错误请求)OnceSingleAsync FIREBASE - System.Net.Http.HttpRequestException: 400 (Bad Request) OnceSingleAsync FIREBASE WCF服务失败-HTTP / 1.1 400错误的请求 - WCF Service failing - HTTP/1.1 400 Bad Request salesforce api错误ReasonPhrase:“错误请求” - salesforce api error ReasonPhrase: 'Bad Request' 无法使用 Xunit 测试端点 - StatusCode: 400, ReasonPhrase: &#39;Bad Request&#39; - Unable to test endpoint with Xunit - StatusCode: 400, ReasonPhrase: 'Bad Request' HTTP 400错误请求错误C# - HTTP 400 Bad Request Error C# 远程服务器返回错误:(400)Bad Request.System.Net.HttpWebResponse Hammock Twitter API - The remote server returned an error: (400) Bad Request.System.Net.HttpWebResponse Hammock Twitter API 在.NET MVC API控制器中对400 post请求的错误请求 - 400 bad request for http post request in .NET MVC API controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM