WebAPI我自己的状态代码

时间:2016-03-13 23:48:35

标签: c# asp.net-web-api asp.net-web-api2 asp.net-web-api-routing

我使用WebApi 2并且想要使用自己的状态代码(从452到499)来处理错误的请求。

即:用户没有必需的角色 - 代码为452,操作未完成#1 - 代码为453,原因为#2 - 代码为454等...

问题是我返回HttpResponseMessage并且它只允许返回HttpStatusCode枚举,它只有预定义(标准)状态代码

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");
}

我的想法好不好?如果好,那我该怎么解决呢?

0 个答案:

没有答案
相关问题