0

Why do I get All instead of the Single result when I do an HTTP request for a web API like this:

http://localhost/HR/api/camps/2

I think the second method should be executed but the first one is executed instead:

  public IHttpActionResult GetCamps()
    {
        var  camps = _context.CAMPs.ToList()
                    .Select(Mapper.Map<CAMP, CampDTO>);
        return Ok(camps);

    }

    public IHttpActionResult GetCamp(int campCode)
    {
        var camp = _context.CAMPs
            .SingleOrDefault(c => c.CAMP_CODE == campCode);

        if (camp == null)
            return NotFound();

        return Ok(Mapper.Map<CAMP, CampDTO>(camp));

    }
Draken
  • 3,134
  • 13
  • 34
  • 54
Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392
  • 1
    Depends entirely on how your routes are set up, which we don't know. – Damien_The_Unbeliever Aug 16 '18 at 10:12
  • @Damien_The_Unbeliever I didn't make any custom routing here – Anyname Donotcare Aug 16 '18 at 10:12
  • you can specify route on action [Route("api/camps/{campCode}")] like https://stackoverflow.com/questions/12775590/routing-with-multiple-get-methods-in-asp-net-web-api/40397258#40397258 – Lalji Dhameliya Aug 16 '18 at 10:16
  • or if you don't want to specify route u can try this .. http://localhost/HR/api/camps?campCode=2 – mongesh madhavan Aug 16 '18 at 10:18
  • 1
    Routes are matched in the order as they are declared and when there is a match it won't check further routes, so even if you don't explicitly declare, i guess since the first method can also match with the request it just executes that (to prove that may be you can change the ordering of the methods). But it is always good to explicitly specify the routes. – Thangadurai Aug 16 '18 at 10:18

1 Answers1

4

If you've created a brand new ASP.Net (not core) Web API project in Visual Studio 2017 (I'm on 15.8.0), this is the default contents of WebApiConfig.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace WebApplication1
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

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

You'll notice that it has a mapping for an id parameter, but no such mapping for campCode (hopefully, that isn't unexpected).

You can either add a separate route or change your parameter name. Or supply campCode as a query string parameter instead.


For any "why doesn't it call method X instead of Y", you should realise it's always a question of routes. There's a handy Route Debugger by Phil Haack which you can always add in and help with experimenting/answering this sort of question.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448