Asp.net 端点适用于失眠或默认表单行为,但不适用于提取 API

Asp.net endpoint works with insomnia or default form behavior, but not with the fetch API

提问人:Literal Garbage 提问时间:4/4/2023 更新时间:4/4/2023 访问量:71

问:

我正在做一个以 React 为前端的 ASP.NET 7 SPA 项目。我有一个特定的端点,旨在成为卖家可以上传产品列表的端点,包括产品的图像。因此,数据与 .问题是,当我使用有效负载和所有 fetch API 发送请求时,我收到错误: 但是,当我通过表单的默认操作行为与在侦听器中使用 fetch API 发出请求时,请求被完美地解析,没有任何问题。multipart/form-dataContent-TypeSystem.IO.IOException: Unexpected end of Stream, the content may have already been read by another component.onSubmit

以下是调用堆栈的异常:

System.IO.IOException: Unexpected end of Stream, the content may have already been read by another component. 
   at Microsoft.AspNetCore.WebUtilities.MultipartReaderStream.ReadAsync(Memory`1 buffer, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.WebUtilities.StreamHelperExtensions.DrainAsync(Stream stream, ArrayPool`1 bytePool, Nullable`1 limit, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.WebUtilities.MultipartReader.ReadNextSectionAsync(CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Http.Features.FormFeature.InnerReadFormAsync(CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Http.Features.FormFeature.ReadForm()
   at Shamazon.Controllers.ProductsController.Upload() in /mnt/wwn-0x5000c500c757436c-part1/Workspace/Shamazon/Controllers/ProductsController.cs:line 22

下面是 React 中的 fetch 请求:

const request = await fetch("/api/products/upload", {
     method: "POST",
     headers: {
          "Content-Type": "multipart/form-data; boundary=----WebKitFormBoundaryLhcxqmSmEPRWew71"
     },
     body: JSON.stringify({
          Name: name,
          Description: description,
          Price: price,
          ImageUrl: file
     })
});

以下是 ASP.Net 中的控制器功能:

[HttpPost("upload")]
[DisableFormValueModelBindingAttribute]
public async Task<IActionResult> Upload()
{
     Console.WriteLine("Form Body");
     try
     {
          var thing = HttpContext.Request.Form.ToArray();

          foreach (var item in thing)
          {
               Console.WriteLine(item);
          }
     }
     catch (System.Exception exc)
     {
          Console.WriteLine("Failure of form.");
          Console.WriteLine(exc);
     }

     Console.WriteLine("Form File Name");
     try
     {
          Console.WriteLine(HttpContext.Request.Form.Files[0].FileName);
     }
     catch (System.Exception exc)
     {
          Console.WriteLine("Failure of file.");
          Console.WriteLine(exc);
     }

     return StatusCode(202, new { test = "bruh" });
}

函数上的属性:是我找到的所有帖子中通常所说的问题的解决方案。这个想法是控制器将数据绑定到属性禁用的模型(顾名思义)。这是行不通的,但我会包括它的定义,因为我可能没有正确地实现它。[DisableFormValueModelBindingAttribute]

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class DisableFormValueModelBindingAttribute : Attribute, IResourceFilter
{
     public void OnResourceExecuting(ResourceExecutingContext context)
     {
          var formValueProviderFactory = context.ValueProviderFactories
               .OfType<FormValueProviderFactory>()
               .FirstOrDefault();
          if (formValueProviderFactory != null)
          {
               context.ValueProviderFactories.Remove(formValueProviderFactory);
          }

          var formFileValueProviderFactory = context.ValueProviderFactories
               .OfType<FormFileValueProviderFactory>()
               .FirstOrDefault();
          if (formFileValueProviderFactory != null)
          {
               context.ValueProviderFactories.Remove(formFileValueProviderFactory);
          }

          var jqueryFormValueProviderFactory = context.ValueProviderFactories
               .OfType<JQueryFormValueProviderFactory>()
               .FirstOrDefault();
          if (jqueryFormValueProviderFactory != null)
          {
               context.ValueProviderFactories.Remove(jqueryFormValueProviderFactory);
          }
     }

     public void OnResourceExecuted(ResourceExecutedContext context)
     {
     }
}
asp.net http 异常 asp.net-web-api fetch-api

评论


答: 暂无答案