summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI.Web/Framework/CommaDelimitedModelBinderProvider.cs
blob: 1b3f0073677bde15ef780c54e2c7546a3f3a2a6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;
using Microsoft.AspNetCore.Mvc.ModelBinding;

namespace StardewModdingAPI.Web.Framework
{
    /// <summary>Provides comma-delimited model binds for mapping parameters.</summary>
    /// <remarks>Derived from <a href="https://stackoverflow.com/a/43655986/262123" />.</remarks>
    public class CommaDelimitedModelBinderProvider : IModelBinderProvider
    {
        /*********
        ** Public methods
        *********/
        /// <summary>Creates a model binder based on the given context.</summary>
        /// <param name="context">The model binding context.</param>
        public IModelBinder GetBinder(ModelBinderProviderContext context)
        {
            // validate
            if (context == null)
                throw new ArgumentNullException(nameof(context));

            // get model binder
            return context.Metadata.IsEnumerableType && !context.Metadata.ElementMetadata.IsComplexType
                ? new CommaDelimitedModelBinder()
                : null;
        }
    }
}