不支持“Microsoft.AspNetCore.Http.IFormFile.Headers”上的集合类型“Microsoft.AspNetCore.Http.IHeaderDictionary”

Kam*_*ami 3 c# ajax asp.net-core

我正在尝试使用 Ajax 将类列表返回到我的视图\n这是我的 ajax

\n
   $(document).ready(function () {\n\n        debugger;\n\n        $.ajax({\n            url: '/Product/GetCard/',\n            type: 'GET',\n            dataType: "json",\n            contentType: "application/json; charset=utf-8",\n            \n            \n            success: function (result) {\n                debugger;\n\n                var price = 0;\n                var cnt = $(result).length;\n                for (var i = 0; i < cnt; i++) {\n\n                    debugger;\n                    var newItem = '<div class="mini-cart-product-box pad-top20">' +\n                        '<div class="cart-img-box">' +\n                        ' <figure>' +\n                        ' <img src="/Product/BlogImage/' + result[i].prdId + '"' + ' alt="">' +\n                        '</figure>' +\n                        '</div>' +\n                        '<div class="cart-price-box">' +\n                        '<h5><a href="product-detail.html">' + result[i].name + '</a></h5>' +\n                        '<p>' +\n                        result[i].count + 'x' + result[i].price + '</p>' +\n                        '</div>' +\n                        '<div class="cancle-box">' +\n                        ' <a href="/Product/RemoveFromCart/' + result[i].id + '">x</a>' +\n                        '</div>' +\n                        '</div >';\n\n                    $('#ccard').prepend(newItem);\n\n                    $('#meme').html(' <span class="flaticon-cart"></span>' +\n                        '<span class= "count">' + cnt + '</span>');\n                    price = price + result[i].price;\n\n                }\n\n                $('#pri').html('<span class="furgan-Price-currencySymbol">\xd8\xb1\xdb\x8c\xd8\xa7\xd9\x84</span>' + price);\n\n            },\n           \n            error: function (err) {\n                console.log(err)\n                alert(err)\n            },\n           \n        });\n\n\n\n\n\n    })\n
Run Code Online (Sandbox Code Playgroud)\n

这称为

\n
   [AllowAnonymous]\n    [HttpGet]\n    public JsonResult GetCard(string Idis)\n    {\n        var CardIdis = Request.HttpContext.Request.Cookies["UserCard"];\n\n        if (CardIdis != null)\n        {\n            var Pros = _ProductConnections.GetProducts(CardIdis);\n           \n            return Json(Pros);\n        }\n\n        return Json(new List<ProductVm>());\n\n    }\n
Run Code Online (Sandbox Code Playgroud)\n

一切顺利并且返回,但最后成功函数没有被调用\n并且错误消息是

\n
An unhandled exception occurred while processing the request.\nNotSupportedException: The collection type 'Microsoft.AspNetCore.Http.IHeaderDictionary' on 'Microsoft.AspNetCore.Http.IFormFile.Headers' is not supported.\nSystem.Text.Json.JsonClassInfo.GetElementType(Type propertyType, Type parentType, MemberInfo memberInfo, JsonSerializerOptions options)\n\nStack Query Cookies Headers Routing\nNotSupportedException: The collection type 'Microsoft.AspNetCore.Http.IHeaderDictionary' on 'Microsoft.AspNetCore.Http.IFormFile.Headers' is not supported.\nSystem.Text.Json.JsonClassInfo.GetElementType(Type propertyType, Type parentType, MemberInfo memberInfo, JsonSerializerOptions options)\nSystem.Text.Json.JsonClassInfo.CreateProperty(Type declaredPropertyType, Type runtimePropertyType, Type implementedPropertyType, PropertyInfo propertyInfo, Type parentClassType, JsonConverter converter, JsonSerializerOptions options)\nSystem.Text.Json.JsonClassInfo.AddProperty(Type propertyType, PropertyInfo propertyInfo, Type classType, JsonSerializerOptions options)\nSystem.Text.Json.JsonClassInfo..ctor(Type type, JsonSerializerOptions options)\nSystem.Text.Json.JsonSerializerOptions.GetOrAddClass(Type classType)\nSystem.Text.Json.JsonPropertyInfo.get_ElementClassInfo()\nSystem.Text.Json.JsonSerializer.HandleObject(JsonPropertyInfo jsonPropertyInfo, JsonSerializerOptions options, Utf8JsonWriter writer, ref WriteStack state)\nSystem.Text.Json.JsonSerializer.WriteObject(JsonSerializerOptions options, Utf8JsonWriter writer, ref WriteStack state)\nSystem.Text.Json.JsonSerializer.Write(Utf8JsonWriter writer, int originalWriterDepth, int flushThreshold, JsonSerializerOptions options, ref WriteStack state)\nSystem.Text.Json.JsonSerializer.WriteAsyncCore(Stream utf8Json, object value, Type inputType, JsonSerializerOptions options, CancellationToken cancellationToken)\nMicrosoft.AspNetCore.Mvc.Infrastructure.SystemTextJsonResultExecutor.ExecuteAsync(ActionContext context, JsonResult result)\nMicrosoft.AspNetCore.Mvc.Infrastructure.SystemTextJsonResultExecutor.ExecuteAsync(ActionContext context, JsonResult result)\nMicrosoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0<TFilter, TFilterAsync>(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)\nMicrosoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)\nMicrosoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext<TFilter, TFilterAsync>(ref State next, ref Scope scope, ref object state, ref bool isCompleted)\nMicrosoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()\nMicrosoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)\nMicrosoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)\nMicrosoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)\nMicrosoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()\nMicrosoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)\nMicrosoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)\nMicrosoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)\nMicrosoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)\nMicrosoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)\n
Run Code Online (Sandbox Code Playgroud)\n

我没有这个问题,今天开始,顺便说一句,我使用 cookie 来保存我想要退回的产品 idis

\n

Fei*_*Han 7

NotSupportedException:不支持“Microsoft.AspNetCore.Http.IFormFile.Headers”上的集合类型“Microsoft.AspNetCore.Http.IHeaderDictionary”。

根据异常消息和您共享的代码,代码似乎使用 进行 JSON 序列化IFormFile,这导致了问题。请检查您的 Product 视图模型类是否包含IFormFiletype 属性。

此外,如果您想返回带有产品信息的 Json 结果,您可以包含图像 URL 或产品的 base64 编码图像,而不是 FormFile。

  • 为了获取产品数据,如果可能的话,您可以在 IFormFile 类型属性上使用“[JsonIgnore]”。 (2认同)