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 4052458
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:19:21+00:00 2026-05-20T14:19:21+00:00

In my API, I’d like to have routes like GET /api/v1/widgets/1,2,3 and GET /api/v1/widgets/best-widget,major-widget,bob-the-widget

  • 0

In my API, I’d like to have routes like GET /api/v1/widgets/1,2,3 and GET /api/v1/widgets/best-widget,major-widget,bob-the-widget

public class WidgetsController : MyApiController
{
    public ActionResult Show(IEnumerable<int> ids)
    {

    }

    public ActionResult Show(IEnumerable<string> names)
    {

    }
}

I’ve got routes set up to get me to the action, but I can’t figure out how to turn 1,2,3 into new List<int>(){1, 2, 3} and so on. Of course, I could just take a string and parse it in my action, but I’d like to avoid going that route.

One thing that came to mind was to put something in the OnActionExecuting method, but then I wasn’t sure exactly what to put in there (I could hack something together, obviously, but I’m trying to write something reusable.)

The main questions I have are how to know whether I need to do anything at all (sometimes the ValueProviders upstream will have figured everything out), and how to handle figuring out the type to cast to (e.g., how do I know that in this case I need to go to a collection of ints, or a collection of strings, and then how do I do that?)

By the way, I had the idea of implementing a ValueProvider as well, but got lost on similar questions.

  • 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-05-20T14:19:22+00:00Added an answer on May 20, 2026 at 2:19 pm

    I can’t figure out how to turn 1,2,3 into new List(){1, 2, 3} and so on.

    To avoid polluting each controller action that needs to receive this parameter I would recommend a custom model binder:

    public class IdsModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var result = base.BindModel(controllerContext, bindingContext);
            var ids = bindingContext.ValueProvider.GetValue("ids");
            if (ids != null)
            {
                return ids.AttemptedValue
                          .Split(',')
                          .Select(id => int.Parse(id))
                          .ToList();
            }
            return result;
        }
    }
    

    and then register the model binder in Application_Start:

    ModelBinders.Binders.Add(typeof(IEnumerable<int>), new IdsModelBinder());
    

    and finally your controller action might look like this (and from what I can see in your question it already does look like this :-)):

    public ActionResult Show(IEnumerable<int> ids)
    {
        ...
    }
    

    and the custom model binder will take care for parsing the ids route token to the corresponding IEnumerable<int> value.

    You could do the same with the IEnumerable<string> where you would simply remove the .Select clause in the corresponding model binder.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

API: namespace ClassLibrary1 { public class Class1 { public static string Test(string input) {
http://api-public.addthis.com/url/shares.json?url=http://google.com I need to get the number of shares as a php string. (echo)
The API gives the code as: public function up() { $this->addColumn('table_name', 'column_name', 'string', $options);
I am using web api with ASP.NET MVC 4. I have the following named
I currently have an API script that returns JSON. It has worked up until
//API class Node class Person extends Node object Finder { def find[T <: Node](name:
Which API would I need to use to get 'n' recent tweets of a
The API says that the Alert Dialog can have one, two or three buttons,
Frapi API Documentation Page not workign properly after i have synchronize. but all other
android API 8. I'd like to show some data in a listView and in

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.