Web API Default Route does not work when it receives a parameter

1

I am using Web Api 2.0 together with MVC 5

This is my WebApiConfig

  config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

This is my controller:

public class PartesController : ApiController
{
    public IHttpActionResult Get(int? id)
    {
        try
        {
            using (HornosContext db = new HornosContext())
            {
                if (id == null || id == 0)
                {
                    return Ok(db.Partes.ToList());
                }
                else
                {
                    return Ok(db.HornosPartes.Where(ph => ph.IdHorno == id).Select(ph => ph.Parte).ToList());
                }
            }
        }
        catch (Exception ex)
        {
            return InternalServerError(ex);
        }
    }
}
  • When calling http://blabla/api/partes/1 It works
  • Calling http://blabla/api/partes gives me 404 NOT FOUND

If I remove the parameter of the action:

public IHttpActionResult Get()
    {
        try
        {
            using (HornosContext db = new HornosContext())
            {
                return Ok(db.Partes.ToList());
            }
        }
        catch (Exception ex)
        {
            return InternalServerError(ex);
        }
    }

and I'll call the method like this: http://blabla/api/partes It works.

Why is the Web API not identifying the id as an optional value?

EDIT:

I know I can add attributes and more actions, but being something so simple, I would like this to work with the default routes.

In the question suggested as duplicate the problem has to do with the order of registration of the routing configurations, here I resolved it by specifying a predetermined value to the parameter of the action (see answer below)

    
asked by Tuco 04.04.2016 в 15:13
source

2 answers

3

I just had to specify a default value to the method parameter and it worked

 public IHttpActionResult Get(int? id = null)
 {
  .....
    
answered by 04.04.2016 / 15:43
source
3

If you use Web Api 2 use attributes to map using the [Route] of this way you could create two action receiving or not the id

Attribute Routing in ASP.NET Web API 2

public class PartesController : ApiController
{
    [Route("api/partes/{id:int}")]
    [HttpGet]
    public IHttpActionResult Get(int id)
    {
        try
        {
            using (HornosContext db = new HornosContext())
            {
                return Ok(db.HornosPartes.Where(ph => ph.IdHorno == id).Select(ph => ph.Parte).ToList());
            }
        }
        catch (Exception ex)
        {
            return InternalServerError(ex);
        }
    }

    [Route("api/partes")]
    [HttpGet]
    public IHttpActionResult GetAll()
    {
        try
        {
            using (HornosContext db = new HornosContext())
            {
                return Ok(db.Partes.ToList());
            }
        }
        catch (Exception ex)
        {
            return InternalServerError(ex);
        }
    }
}
    
answered by 04.04.2016 в 15:25