Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

845 B
Raw Blame
using Grand.Infrastructure.ModelBinding;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;

namespace Grand.Infrastructure.Extensions;

public static class MvcOptionsExtensions
{
    public static void UseJsonBodyModelBinderProviderInsteadOf<T>(this MvcOptions mvcOptions)
        where T : IModelBinderProvider
    {
        var replacedModelBinderProvider = mvcOptions.ModelBinderProviders.OfType<T>().FirstOrDefault();
        if (replacedModelBinderProvider == null) return;
        var index = mvcOptions.ModelBinderProviders.IndexOf(replacedModelBinderProvider);
        var customProvider = new JsonBodyModelBinderProvider(replacedModelBinderProvider);
        mvcOptions.ModelBinderProviders.Remove(replacedModelBinderProvider);
        mvcOptions.ModelBinderProviders.Insert(index, customProvider);
    }
}