locked
Always returns null RRS feed

  • Question

  • User-1129879462 posted

    Though WebApiConfig is hitting breakpoint, breakpoint placed on method test() never hits and call to http://localhost/api/Projects/test always returns null!!

    [HttpGet]

    public string test()
    {
    return "test";
    }

    config.Routes.MapHttpRoute(
    name: "test",
    routeTemplate: "api/Projects/test/",
    defaults: new { controller = "Project", action = "test" },
    constraints: new { httpMethod = new HttpMethodConstraint("GET") }
    );

    Any help would be appreciated

    Wednesday, February 8, 2017 8:11 AM

Answers

  • User-2057865890 posted

    Hi Krisrajz,

    The order in which Route objects appear in the Routes collection is significant. Route matching is tried from the first route to the last route in the collection. When a match occurs, no more routes are evaluated. 

    config.Routes.MapHttpRoute(
         name: "test",
         routeTemplate: "api/Projects/test/",
         defaults: new { controller = "Project", action = "test" },
         constraints: new { httpMethod = new HttpMethodConstraint("GET") }
        );
    
    config.Routes.MapHttpRoute(
         name: "DefaultApi",
         routeTemplate: "api/{controller}/{id}",
         defaults: new { id = RouteParameter.Optional }
    );
    public class ProjectController : ApiController
    {
        [HttpGet]
        public string test()
        {
           return "test";
        }
    }


    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 8, 2017 9:21 AM

All replies

  • User197322208 posted

    Please show the controller definition.

    Wednesday, February 8, 2017 8:22 AM
  • User-1129879462 posted

    public class ProjectController : ApiController

    Wednesday, February 8, 2017 8:56 AM
  • User-1129879462 posted

    There are bunch of few other methods working fine!

    Wednesday, February 8, 2017 8:59 AM
  • User-2057865890 posted

    Hi Krisrajz,

    The order in which Route objects appear in the Routes collection is significant. Route matching is tried from the first route to the last route in the collection. When a match occurs, no more routes are evaluated. 

    config.Routes.MapHttpRoute(
         name: "test",
         routeTemplate: "api/Projects/test/",
         defaults: new { controller = "Project", action = "test" },
         constraints: new { httpMethod = new HttpMethodConstraint("GET") }
        );
    
    config.Routes.MapHttpRoute(
         name: "DefaultApi",
         routeTemplate: "api/{controller}/{id}",
         defaults: new { id = RouteParameter.Optional }
    );
    public class ProjectController : ApiController
    {
        [HttpGet]
        public string test()
        {
           return "test";
        }
    }


    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 8, 2017 9:21 AM
  • User-1129879462 posted

    Awesome! After I changed the order in WebApiConfig, it worked like plum!

    I appreciate your timely replies.

    Wednesday, February 8, 2017 9:48 AM