0

I use WebApi 2 and have an idea to use own status codes (from 452 till 499) for bad request.

i.e: user has no required role - code is 452, operation is not completed by reason #1 - code is 453, by reason #2 - code is 454 etc...

Problem is I return HttpResponseMessage and it allows to return only HttpStatusCode enumeration, which has only pre-defined (standard) status codes

public async Task<HttpResponseMessage> PasswordRecoveryAsync(PasswordRecoveryApiModel model)
{
    var user = await UserManager.FindByNameAsync(model.Username);
    if (user == null)
        return Request.CreateErrorResponse(HttpStatusCode.NotFound, "User with such username is not found");

    if (user.AspNetRoles.Where(p=>p.Name == Models.Roles.Editorial).Count() == 0)
        return Request.CreateErrorResponse(HttpStatusCode.NotFound, "...")

    ....

    return Request.CreateResponse<string>(HttpStatusCode.OK, "Confirmation token is sent to your email");
}

Is my idea good or not? If good, then how do I solve it?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Oleg Sh
  • 8,496
  • 17
  • 89
  • 159
  • 2
    Possible duplicate of [Return Custom HTTP Status Code from WebAPI 2 endpoint](http://stackoverflow.com/questions/23399272/return-custom-http-status-code-from-webapi-2-endpoint) – CodeCaster Mar 14 '16 at 00:02
  • See [Return Custom HTTP Status Code from WebAPI 2 endpoint](http://stackoverflow.com/questions/23399272/return-custom-http-status-code-from-webapi-2-endpoint), but you really don't want to use custom, unregistered status codes. Use an application-specific response message instead (e.g. a status code in a JSON payload). – CodeCaster Mar 14 '16 at 00:03
  • why custom unregistered status code is bad idea? – Oleg Sh Mar 14 '16 at 00:14
  • 2
    http://programmers.stackexchange.com/questions/218080/should-i-make-up-my-own-http-status-codes-a-la-twitter-420-enhance-your-calm – CodeCaster Mar 14 '16 at 00:16
  • Using you own Codes goes against **Http Standards**. As suggested before you should use standard http status codes and include custom codes in the payload of your result. – Nkosi Mar 14 '16 at 02:28
  • Asking a question of type "is this a good idea" is perhaps better fit on other sites. Pls check [help] – G. Stoynev Mar 14 '16 at 03:19
  • @Nkosi, but client should understand type of Response Code (first number), it's rule. Why if I use 499 for "client error" it goes against Http Standards? – Oleg Sh Mar 14 '16 at 22:53
  • When you start using custom code it can lead to confusion. take a look at some of the unofficial code in wikipedia https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error – Nkosi Mar 15 '16 at 00:23

0 Answers0