The default setting in the MVC 4 Web API

I am curious why ApiController handles default parameter values ​​for actions differently than a "normal" controller.

This code works just fine, requesting / Test means the page is getting a value of 1

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

This code does not work when a request is made in / api / Values. He does not work:

"The parameter dictionary contains a null entry for the page parameter of the non-element type" System.Int32 "for the method" System.Collections.Generic.IEnumerable`1 [System.String] Get (Int32) 'in' MvcApplication1.Controllers.Controllers.ValuesController ' . The optional parameter must be a reference type, a null type, or 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?

+5
2

[FromUri] [FromForm].

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

Webapi, , ASP MVC. , , , . , , , , ModelBinding . , , , , .

http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx, WebAPI ASP MVC http://blogs.msdn.com/b/jmstall/archive/2012/04/18/mvc-style-parameter-binding-for-webapi.aspx

+5

Nullable<T>:

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

All Articles