P.K.
P.K.

Reputation: 1772

C# Web API2: simple routing with integer parameter is not working

I have a simple controler. I starts 2 URLs, one is working the second is not. Im using VS 2013 EXPRESS :).

http://localhost:53416/api/payments/ - works and displays "1" http://localhost:53416/api/payments/2 - doesn't work and displays error HTTP Error 404.0 - Not Found ( no "2" is displayed).

What is wrong with second action method?

Controler's code

[RoutePrefix("api/payments")]
    public class MvbePaymentController : ApiController
    {
        [Route("")]
        [HttpGet]
        public IHttpActionResult GetPayments()
        {                            
            return Ok("1");
        }    
        [Route("{id:int}")]
        [HttpGet]
        public IHttpActionResult GetPayments(int invoiceId)
        {                        
            return Ok("2");
        }    
    }

WebApiCOnfig:

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {

            config.Filters.Add(new ValidateModelStateAttribute());                                             

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

Upvotes: 0

Views: 26

Answers (1)

Martin Beeby
Martin Beeby

Reputation: 4599

I think your GetPayments code should have the parameter named id rather than invoiceId.

public IHttpActionResult GetPayments(int id)
    {                        
        return Ok("2");
    }    

Upvotes: 1

Related Questions