Search code examples
c#asp.net-web-apinewrelic

Web API Status Endpoint (GET and POST)


We're using New Relic to monitor many of our web applications.

This gives us availability monitoring, which allows you to specify a particular endpoint in your application that can be "pinged" to make sure you application is "alive" - obviously quite useful, but there is a small catch.

I noticed that the ping request I get from New Relic is not always a POST, it is sometimes a GET, which results in my endpoint throwing a 405 HttpStatusMessage : Method not allowed.

No problem - I figured I'd just configure my endpoint to respond to both :

[Route("status")]
public class StatusController : ApiController
{
    public IHttpActionResult Post()
    {
        return Ok();
    }

    public IHttpActionResult Get()
    {
        return Ok();
    }
}

Now granted, this does work, but seems like a lot of trouble for such a simple task.

I'm curious - is there a cleaner or better way of doing this, that I just haven't seen yet?


Solution

  • You can get your method to accept both POST and GET

    // Route /status to this controller
    [RoutePrefix("status")]
    public class StatusController : ApiController
    {
        [HttpGet] // accept get
        [HttpPost] // accept post
        [Route("")] // route default request to this method.
        public IHttpActionResult Get()
        {
            return Ok();
        }
    }