WebApi special exception when "does not support http method"

I have a simple controller:

   public class UsersController : ApiController
    {
        [HttpPost]
        [AllowAnonymous]
         public HttpResponseMessage Login([FromBody] UserLogin userLogin)
         {
             var userId = UserCleaner.Login(userLogin.MasterEntity, userLogin.UserName, userLogin.Password, userLogin.Ua);
             if (userId == null) return Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "User not authorized");
             return Request.CreateResponse(HttpStatusCode.OK, Functions.RequestSet(userId)); 

         }
   }

      

As you can see, only POST is currently available.

But when I call GET in the browser (just for checking):

http://royipc.com:88/api/users

I get:

{"Message": "The requested resource does not support the http 'GET' method." }

It's clear to me why this is happening. But I want to return a custom exception when this happens.

The other answers here on SO do not show how I can relate to a situation like this (not the way I found it)

Question

How (and where) should I catch this situation and return a custom exception (HttpResponseMessage)?

N.B.

I don't want to add a dummy GET method just for "catch and throw". tomorrow there might be a GET method. I just want to catch this exception and return my OWN!

+3


source to share


2 answers


You may need to inherit the ApiControllerActionSelector class, which is used by the web API to select the desired action.

then you can replace the default IHttpActionSelector with your new action selector. config.Services.Replace(typeof(IHttpActionSelector), new MyActionSelector());



check this url for a complete example: http://www.strathweb.com/2013/01/magical-web-api-action-selector-http-verb-and-action-name-dispatching-in-a-single-controller /

+3


source


You can create custom exclusion filters in ASP.Net WebAPI. An exception filter is a class that implements an interface IExceptionFilter

. To create a custom exclusion filter, you can either implement the interface IExceptionFilter

yourself or create a class that inherits from the built-in class ExceptionFilterAttribute

. In a later approach, all you have to do is override the method OnException()

and hook up some custom implementation to it.

public class MyExceptionFilter:ExceptionFilterAttribute
{
    public override void OnException(HttpActionExecutedContext context)
    {
        HttpResponseMessage msg = new HttpResponseMessage(HttpStatusCode.InternalServerError)
        {
            Content = new StringContent("An unhandled exception was thrown by the Web API controller."),
            ReasonPhrase = "An unhandled exception was thrown by the Web API controller."
        };
        context.Response = msg;
    }
}

      

you most likely want to check for conditions and throw an exact exception, but this is an example.

To use an exception class, you can register it in Global.asax or as an attribute for a specific class or method.



public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        GlobalConfiguration.Configuration.Filters.Add(new WebAPIExceptionsDemo.MyExceptionFilter());
        AreaRegistration.RegisterAllAreas();
        ...
    }
}

      

or

[MyExceptionFilter]
public class UsersController : ApiController
{
...
}

      

0


source







All Articles