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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:52:27+00:00 2026-06-02T08:52:27+00:00

I am curious why the ApiController handles default parameter values on actions differently than

  • 0

I am curious why the ApiController handles default parameter values on actions differently than a ‘regular’ Controller.

This code works just fine, request to /Test means page gets value 1

public class TestController : Controller
{
    public ActionResult Index(int page = 1)
    {
        return View(page);
    }
}

This code doesn’t work when a request is made to /api/Values. It fails with:

“The parameters dictionary contains a null entry for parameter ‘page’ of non-nullable type ‘System.Int32’ for method ‘System.Collections.Generic.IEnumerable`1[System.String] Get(Int32)’ in ‘MvcApplication1.Controllers.Controllers.ValuesController’. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.”

public class ValuesController : ApiController
{
    public IEnumerable<string> Get(int page = 1)
    {
        return new string[] { page.ToString() };
    }      
}

Any hints on why this is?

  • 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-02T08:52:30+00:00Added an answer on June 2, 2026 at 8:52 am

    Try adding the [FromUri] or [FromForm] parameter attribute.

    public class ValuesController : ApiController
    {
        public IEnumerable<string> Get([FromUri]int page = 1)
        {
            return new string[] { page.ToString() };
        }      
    }
    

    Mike Stall has two good posts about parameter binding in Webapi which does not work as it does in ASP MVC. The big problem to get used to is that you can only read the request body once in your pipeline. So if you need to read more than 1 complex object as a parameter, you probably need to resort to ModelBinding by parameter. I had a problem similar to yours when I was reading the content body earlier in the pipeline for logging purposes and did not realize about the read once restriction above and had to solve with my own custom model binder.

    Explains model binding at http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx and then suggests a way to make WebAPI model binding more like ASP MVC http://blogs.msdn.com/b/jmstall/archive/2012/04/18/mvc-style-parameter-binding-for-webapi.aspx

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

Sidebar

Related Questions

Just curious (maybe this is an implementation detail so you may not know) but
Curious if anything jumps out here. But when I run this code block :
Curious about how developers more experienced with Django than I might handle this: I
Just curious - is there a package or dataset somewhere containing values for physical
Just curious, why can't I have/see my bg img on this span, unless I
just curious if this will actually run, I modelled it after an example from
Just curious where CPAN installs all the Perl modules by default in OS X.
Just curious as to why this bit of css select, input, textarea, button {
Just curious to know what the best practice would be for something like this:
Curious, what happens when you return keyword this from a struct in C#? For

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.