0

How can I configure routing in ASP.NET Web API to route it to specific method in controller with GET method?

http://mysite/healthcheck

Registration in WebAPiConfig looks like this:

    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "HealthCheck",
        routeTemplate: "healthcheck",
        defaults: new { action =" DefaultAction" }
    );

The controller looks like this:

[RoutePrefix("healthcheck")]
public class HealthCheckController : ApiController
{
    [HttpGet]
    [ActionName("DefaultAction")]
    public HttpResponseMessage GetHealthCheckStatus()
    {
        return Request.CreateResponse(HttpStatusCode.OK);
    }    
}

I get Not Found instead of Ok when hitting that URL. Any help would be appreciated

UPDATE Thank you for all the suggestions, I checked them all and none works. The route debugger shows no matches. I am putting this on hold for a while.

4 Answers 4

2

In your Startup.cs register attribute routes:

config.MapHttpAttributeRoutes();

And in the controller:

[RoutePrefix("healthcheck")]
public class HealthCheckController : ApiController
{
    [HttpGet]
    [Route]
    public IHttpActionResult GetHealthCheckStatus()
    {
        return Ok();
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your answer but it still does not work.
Did you remove the HealthCheck-route from the HttpRouteCollection ?
Do you mean "config.Routes.MapHttpRoute( name: "HealthCheck" ... ) ? Yes I did. It did not help
Ok, weird. Works here :I
After having a lunch break, it started working. Thanks a lot!
2

Remove this in config

config.Routes.MapHttpRoute(
    name: "HealthCheck",
    routeTemplate: "healthcheck",
    defaults: new { action =" DefaultAction" }
);

and do this in controller

public class HealthCheckController : ApiController
{
    [AcceptVerbs("GET")]
    [Route("healthcheck")]
    public HttpResponseMessage GetHealthCheckStatus()
    {
        return Request.CreateResponse(HttpStatusCode.OK);
    }
}

2 Comments

Thank you for your answer. But it did not help
There might be some problem with your configrations \
1

I haven't tested it for web-api, but tested it for MVC, I think it should work the same.

In route registration you should ensure, that this route comes before the standard api route, because route order matters:

public static void RegisterRoutes(RouteCollection routes) {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "HealthCheck",
            "healthcheck",
            new {Controller = "Default", action = "Index"}
        );

        routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            new {
                controller = "Home",
                action = "Index",
                id = UrlParameter.Optional
            }
        );
    }

In the controller you may not prefix the controllers and action names, but to specify correct controller name and action name in the template:

public class DefaultController : Controller
{

    [HttpGet]
    public ActionResult Index()
    {
        return Content("I'm hit");
    }
}

If you still having problems, then you can read this article and enable route debugging too see which route is invoked and what are the params.

1 Comment

Probably there is something else wrong in your project that makes routing go wild. I would suggest you to create a small project and check if you can reproduce the issue you have described there. If yes - put it here, if no - check what's different in your code.
1

Since you are enabling attribute routing, you can specify the action as the default endpoint for the controller using RoutePrefix by using [Route("")] with empty route template.

[RoutePrefix("healthcheck")]
public class HealthCheckController : ApiController {
    [HttpGet]
    [Route("")] // Matches GET http://mysite/healthcheck
    public IHttpActionResult GetHealthCheckStatus() {
        return Ok();
    }
}

With the above then there would be no need for the convention-based HealthCheck route from the OP. It can be removed safely from WebApiConfig

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.