4

Below is WebAPI.

[RoutePrefix("api/customer")]
public class CustomerController : ApiController
{
    [Route("{id:int:min(1)}/")]
    public HttpResponseMessage Get(int id)
    {
        //my stuff
    }
}

If I pass any value less than 1 (say 0 or -1). It returns response body as NUll with HttpStatusCode = 200

The expected response is: HttpStatus Code = 404.

However, if I modify my route as below.

 [RoutePrefix("api/customer")]
public class CustomerController : ApiController
{
    [Route("detail/{id:int:min(1)}/")]
    public HttpResponseMessage Get(int id)
    {
        //my stuff
    }
}

Now if I pass the value less than 1, I get the expected response i.e., 404.

http://localhost:8080/api/customer/detail/-1 returns - 404.(Desired response).

http://localhost:8080/api/customer/-1 returns - Null.(Not correct).

What is causing this & how do I fix this??

Any help/suggestion highly appreciated.

Kgn-web
  • 7,047
  • 24
  • 95
  • 161
  • 2
    If you put a breakpoint in the method when you do not have `/detail` does it run the method you expect or is it hitting some other endpoint? – Craig H Apr 04 '17 at 13:25
  • @CraigH,In that case it is not hitting the API – Kgn-web Apr 04 '17 at 13:27
  • I would hazard that something else is handling it then. I have a similar constraint on several of my API endpoints and they return 404 if they do not match. I assume it is a typo on your part, but your route prefix is "api/customer**s**" and the URLs you have listed is "api/customer"? – Craig H Apr 04 '17 at 13:33
  • @CraigH, I apologize for the type mistake in the post.Please check the updated post. FYI. Route validation are working as expected if the url starts with some string other than paramters as mentioned in the post – Kgn-web Apr 04 '17 at 13:38
  • I've just made a new project with this routing, and it is working as I would expect with or without the `detail` part of the URL so it sounds like there is some other route being hit. If you still have it in there, try commenting out the default routing in WebApiConfig to see if that makes any difference. – Craig H Apr 04 '17 at 14:36
  • I've just re-enabled the default route, and although I still get a 404 response if I pass 0, it now comes from the API and not IIS as happened previously. – Craig H Apr 04 '17 at 14:40
  • @CraigH, I am surprized. Is it possible for you to share the source code of the same?? – Kgn-web Apr 04 '17 at 14:49
  • There's not much to share really. It's 1 controller as per your post, and in WebApiConfig.Register I have only 1 line `config.MapHttpAttributeRoutes();` – Craig H Apr 04 '17 at 14:59
  • @CraigH This is how my WebApi.Config is https://codepaste.net/7me6jf – Kgn-web Apr 04 '17 at 15:00
  • 1
    I would try creating a new, empty, webapi project and creating your controller with a single method and seeing if that works. It really sounds like some other route, or controller, or site is handling the request. Have you copied and pasted the method/ controller and accidentally left a `Route()` or `RoutePrefix()` attribute in there? – Craig H Apr 04 '17 at 15:06
  • @CraigH, no I don't think so. But let me even check in that way (creating a new, empty, webapi project and creating your controller with a single method) – Kgn-web Apr 04 '17 at 15:08
  • 1
    Have you tried removing the Default route from your WebApi.Config? Just to ensure that's not conflicting. You shouldn't really need it if you are using attribute routing. – Woot Apr 04 '17 at 16:08
  • @Woot,Yes you're correct. When I removed the default routing it is giving me the desired response. – Kgn-web Apr 05 '17 at 07:26
  • @Woot, Can you please add why such behaviour??? – Kgn-web Apr 05 '17 at 07:27

1 Answers1

0

You should remove

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

from your WebApi.Config routing. What happens is the route /api/customers/-1 fails the attribute route because it doesn't meet the constraint. This however does not halt it from looking for other matching routes.

If attribute routing was all you were using you would get a 404, however, after this route fails the framework continues looking for a viable route which it finds in your "DefaultApi" route. So it uses customers for the {controller} and -1 for the {id} and passes this to your Get method.

Woot
  • 1,759
  • 1
  • 16
  • 23