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

Viewed 4205

Im trying to return a list of a class to my view using Ajax this is my 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)
            },
           
        });





    })

which calls the

   [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>());

    }

everything goes fine and it hits the returns but at the end the success function does not get called and the error message is

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)

I didnt have this problem it started today btw im using cookies to save the product idis which i want to return

1 Answers

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

Based on the exception message and the code you shared, it seems that the code do JSON serialization with IFormFile, which cause the issue. Please check if your view model class of Product contains a IFormFile type property.

Besides, if you'd like to return Json result with Product info, you can include image URL or base64-encoded image of product instead of a FormFile.

Related