My action method is set to accept only GET requests. If a verb other than GET is sent to it, an 'HTTP 405 Method not allowed' response would be sent back to the client. But I need to log it whenever a verb other than GET is sent to it. The way I am currently trying is by trying to raise an exception (I don't know which one to catch) and then do the logging inside the catch block. My action method is like as follows:
[RoutePrefix("api/Resource")]
public class MyController : ApiController
{
.
.
.
[Route("MyRoute")]
[ResponseType(typeof(IEnumerable<MyResource>))]
[HttpGet]
public async Task<IHttpActionResult> GetMyResourceAsync()
{
try
{
.
.
.
}
catch (WhichException e) // which exception do I catch here if a verb other than GET is sent by a client?
{
MyLogger.Log("Invalid http verb!");
}
}
.
.
.
}
When I try to access the resource using postman, and specify a verb such as POST or PUT, I get an HTTP 405 response, but I am unsure which exception to catch in my action method in order to intercept a non GET request. It need not necessarily be by raising an exception, but any other way to log a non GET request too would do.