Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8063621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:06:13+00:00 2026-06-05T11:06:13+00:00

I found a really nice action filter that converts a comma-separated parameter to a

  • 0

I found a really nice action filter that converts a comma-separated parameter to a generic type list: http://stevescodingblog.co.uk/fun-with-action-filters/

I would like to use it but it will not work for an ApiController, it completely ignore it. Can someone help convert this for Web API use?

[AttributeUsage(AttributeTargets.Method)]
public class SplitStringAttribute : ActionFilterAttribute
{
    public string Parameter { get; set; }

    public string Delimiter { get; set; }

    public SplitStringAttribute()
    {
        Delimiter = ",";
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.ActionParameters.ContainsKey(this.Parameter))
        {
            string value = null;
            var request = filterContext.RequestContext.HttpContext.Request;

            if (filterContext.RouteData.Values.ContainsKey(this.Parameter)
                && filterContext.RouteData.Values[this.Parameter] is string)
            {
                value = (string)filterContext.RouteData.Values[this.Parameter];
            }
            else if (request[this.Parameter] is string)
            {
                value = request[this.Parameter] as string;
            }

            var listArgType = GetParameterEnumerableType(filterContext);

            if (listArgType != null && !string.IsNullOrWhiteSpace(value))
            {
                string[] values = value.Split(Delimiter.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                var listType = typeof(List<>).MakeGenericType(listArgType);
                dynamic list = Activator.CreateInstance(listType);

                foreach (var item in values)
                {
                    try
                    {
                        dynamic convertedValue = TypeDescriptor.GetConverter(listArgType).ConvertFromInvariantString(item);
                        list.Add(convertedValue);
                    }
                    catch (Exception ex)
                    {
                        throw new ApplicationException(string.Format("Could not convert split string value to '{0}'", listArgType.FullName), ex);
                    }
                }

                filterContext.ActionParameters[this.Parameter] = list;
            }
        }

        base.OnActionExecuting(filterContext);
    }

    private Type GetParameterEnumerableType(ActionExecutingContext filterContext)
    {
        var param = filterContext.ActionParameters[this.Parameter];
        var paramType = param.GetType();
        var interfaceType = paramType.GetInterface(typeof(IEnumerable<>).FullName);
        Type listArgType = null;

        if (interfaceType != null)
        {
            var genericParams = interfaceType.GetGenericArguments();
            if (genericParams.Length == 1)
            {
                listArgType = genericParams[0];
            }
        }

        return listArgType;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:06:15+00:00Added an answer on June 5, 2026 at 11:06 am

    What namespace are you using for ActionFilterAttribute? For Web API you need to be using System.Web.Http.Filters namespace and not System.Web.Mvc.

    EDIT

    Here’s a rough conversion, not fully tested.

    SplitStringAttribute

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Net.Http;
    using System.Web.Http.Controllers;
    using System.Web.Http.Filters;
    
    namespace StackOverflowSplitStringAttribute.Infrastructure.Attributes
    {
        [AttributeUsage(AttributeTargets.Method)]
        public class SplitStringAttribute : ActionFilterAttribute
        {
            public string Parameter { get; set; }
    
            public string Delimiter { get; set; }
    
            public SplitStringAttribute()
            {
                Delimiter = ",";
            }
    
            public override void OnActionExecuting(HttpActionContext filterContext)
            {
                if (filterContext.ActionArguments.ContainsKey(Parameter))
                {
                    var qs = filterContext.Request.RequestUri.ParseQueryString();
                    if (qs.HasKeys())
                    {
                        var value = qs[Parameter];
    
                        var listArgType = GetParameterEnumerableType(filterContext);
    
                        if (listArgType != null && !string.IsNullOrWhiteSpace(value))
                        {
                            string[] values = value.Split(Delimiter.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
    
                            var listType = typeof(List<>).MakeGenericType(listArgType);
                            dynamic list = Activator.CreateInstance(listType);
    
                            foreach (var item in values)
                            {
                                try
                                {
                                    dynamic convertedValue = TypeDescriptor.GetConverter(listArgType).ConvertFromInvariantString(item);
                                    list.Add(convertedValue);
                                }
                                catch (Exception ex)
                                {
                                    throw new ApplicationException(string.Format("Could not convert split string value to '{0}'", listArgType.FullName), ex);
                                }
                            }
    
                            filterContext.ActionArguments[Parameter] = list;
                        }
                    }
                }
    
                base.OnActionExecuting(filterContext);
            }
    
            private Type GetParameterEnumerableType(HttpActionContext filterContext)
            {
                var param = filterContext.ActionArguments[Parameter];
                var paramType = param.GetType();
                var interfaceType = paramType.GetInterface(typeof(IEnumerable<>).FullName);
                Type listArgType = null;
    
                if (interfaceType != null)
                {
                    var genericParams = interfaceType.GetGenericArguments();
                    if (genericParams.Length == 1)
                    {
                        listArgType = genericParams[0];
                    }
                }
    
                return listArgType;
            }
    
        }
    }
    

    CsvController

    using System.Web.Http;
    using System.Collections.Generic;
    using StackOverflowSplitStringAttribute.Infrastructure.Attributes;
    
    namespace StackOverflowSplitStringAttribute.Controllers
    {
        public class CsvController : ApiController
        {
    
            // GET /api/values
    
            [SplitString(Parameter = "data")]
            public IEnumerable<string> Get(IEnumerable<string> data)
            {
                return data;
            }
        }
    }
    

    Example request

    http://localhost:52595/api/csv?data=this,is,a,test,joe
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this really nice function that receives your desired proportions and resizes/crops the
I just found out about yield return , I seems really nice. I use
I have searched around a bit, and have not really found a professional type
I really think that here ill found my answers... I try to make a
I found that Rx framework looks really useful for async operations but i cannt
Found a really nice code for Accordion nav and am trying implement on our
MySQLdb is really nice library for handling SQL connections. I found only one problem.
A while back I found a really nice snippet which showed how a file
I'm developing an Android 3.1 tablet application. I've found a really nice widget ViewPager
One of my colleagues uses TextPad, and one feature I found really useful is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.