I want to create extension method for ApiController to be able to return custom content.
My idea is to return custom error with my own details.
I want to return custom error similar to errors returned by OAuthAuthorizationServerProvider
:
{
"error": "invalid_grant",
"error_description": "You have 3 more attempts before Your account will be locked."
}
Inside my ApiController I've added this method:
public IHttpActionResult Test()
{
HttpError err = new HttpError();
err["error"] = "40001";
err["error_description"] = "Something is wrong";
var response = Request.CreateErrorResponse(HttpStatusCode.NotFound, err);
return ResponseMessage(response);
}
This gives me nice looking response:
{
"error": "40001",
"error_description": "Somethis is wrong"
}
I've tried converting this to below extension method:
public static class ApiControllerExtensions
{
public static IHttpActionResult BadRequest(this ApiController apiController, string error, string errorDetails)
{
HttpError err = new HttpError();
err["error"] = error;
err["error_description"] = errorDetails;
var response = apiController.Request.CreateErrorResponse(HttpStatusCode.NotFound, err);
return apiController.ResponseMessage(response);
}
}
but I get error: Cannot access protected internal method 'ResponseMessage' here
I know I can create custom base ApiController and add that method there, but I'd like to create extension method so it will be easier to reuse it in other projects.
How can I return IHttpActionResult from ApiController extension method?