返回异步函数类型以返回成功消息

时间:2014-11-05 10:03:03

标签: asp.net-mvc

我有以下API控制器:

 public async Task<ActionResult> PostRegister(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking this link: <a href=\"\">link</a>");

            }

        }
//there should be a return value here
}

我想要的是返回一条成功消息,告知用户已经添加了该怎么回事?

1 个答案:

答案 0 :(得分:0)

在网络API中,您无需返回ActionResult,而是可以返回HttpResponseMessage

所以它会像这样

public async Task<HttpResponseMessage> PostRegister(RegisterViewModel model)

//Prepare the response message
HttpResponseMessage responseMessage = new HttpResponseMessage();
---
responseMessage.StatusCode = HttpStatusCode.OK;
responseMessage.Content = new StringContent("Success: registered.");

return responseMessage;