c# - 不支持 'Microsoft.AspNetCore.Http.IHeaderDictionary' 上的集合类型 'Microsoft.AspNetCore.Http.IFormFile.Headers'

标签 c# ajax asp.net-core

我正在尝试使用 Ajax 将类列表返回到我的 View 这是我的ajax

   $(document).ready(function () {

        debugger;

        $.ajax({
            url: '/Product/GetCard/',
            type: 'GET',
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            
            
            success: function (result) {
                debugger;

                var price = 0;
                var cnt = $(result).length;
                for (var i = 0; i < cnt; i++) {

                    debugger;
                    var newItem = '<div class="mini-cart-product-box pad-top20">' +
                        '<div class="cart-img-box">' +
                        ' <figure>' +
                        ' <img src="/Product/BlogImage/' + result[i].prdId + '"' + ' alt="">' +
                        '</figure>' +
                        '</div>' +
                        '<div class="cart-price-box">' +
                        '<h5><a href="product-detail.html">' + result[i].name + '</a></h5>' +
                        '<p>' +
                        result[i].count + 'x' + result[i].price + '</p>' +
                        '</div>' +
                        '<div class="cancle-box">' +
                        ' <a href="/Product/RemoveFromCart/' + result[i].id + '">x</a>' +
                        '</div>' +
                        '</div >';

                    $('#ccard').prepend(newItem);

                    $('#meme').html(' <span class="flaticon-cart"></span>' +
                        '<span class= "count">' + cnt + '</span>');
                    price = price + result[i].price;

                }

                $('#pri').html('<span class="furgan-Price-currencySymbol">ریال</span>' + price);

            },
           
            error: function (err) {
                console.log(err)
                alert(err)
            },
           
        });





    })

调用

   [AllowAnonymous]
    [HttpGet]
    public JsonResult GetCard(string Idis)
    {
        var CardIdis = Request.HttpContext.Request.Cookies["UserCard"];

        if (CardIdis != null)
        {
            var Pros = _ProductConnections.GetProducts(CardIdis);
           
            return Json(Pros);
        }

        return Json(new List<ProductVm>());

    }

一切顺利,它会返回,但最后不会调用成功函数 错误信息是

An unhandled exception occurred while processing the request.
NotSupportedException: The collection type 'Microsoft.AspNetCore.Http.IHeaderDictionary' on 'Microsoft.AspNetCore.Http.IFormFile.Headers' is not supported.
System.Text.Json.JsonClassInfo.GetElementType(Type propertyType, Type parentType, MemberInfo memberInfo, JsonSerializerOptions options)

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

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

最佳答案

NotSupportedException: The collection type 'Microsoft.AspNetCore.Http.IHeaderDictionary' on 'Microsoft.AspNetCore.Http.IFormFile.Headers' is not supported.

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

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

关于c# - 不支持 'Microsoft.AspNetCore.Http.IHeaderDictionary' 上的集合类型 'Microsoft.AspNetCore.Http.IFormFile.Headers',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65474768/

相关文章:

c# - 将描边应用于 XAML 中的文本 block

php - jQuery Ajax 请求返回错误和状态 0

asp.net - Swagger 不同 namespace 中具有相同名称的不同类不起作用

c# - 无法在 DrawString 上使用 ttf 文件中的字体

c# - 防止最大化的 WPF 窗口覆盖任务栏

jQuery 类型错误 : 'undefined' is not a function on AJAX submit

c# - 'OFFSET' 附近的语法不正确。 FETCH 语句中 NEXT 选项的无效使用 "in Entity Framework core"

javascript - BAD Reuqest 无法创建和填充列表类型 Microsoft.AspNetCore.Http.IFormFileCollection

c# - 有没有办法在 C#/VB.NET 中伪造继承?

javascript - 当我使用 ajax 从 "echo"PHP 获取 html 代码时,我需要继续执行 JS 代码