简体   繁体   中英

ASP.NET Web API 2 custom routing to specific method

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.

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();
    }
}

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);
    }
}

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM