繁体   English   中英

显示ap.net Web api返回结果到mvc视图

[英]display ap.net web api returned result to mvc view

我使用mvc项目和Web api单独的项目创建Web应用程序,我使用身份来注册用户。 我的问题是当Web API返回结果时,如何在MVC视图中显示结果

这是我的Web API控制器-

    public async Task<IHttpActionResult> Register(RegisterBindingModel model)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };

        IdentityResult result = await UserManager.CreateAsync(user, model.Password);



        if (!result.Succeeded)
        {
            return GetErrorResult(result);
        }

    }

这就是我如何称呼此网络api-

    public async Task<ActionResult> Register(RegisterView model)
    {
        //
        HttpClient client = new HttpClient();
        //The URL of the WEB API Service
        string url = "http://localhost:51493/Api/Account/Register";
        client.BaseAddress = new Uri(url);
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        //

        HttpResponseMessage responseMessage = await client.PostAsJsonAsync(url, model);
        if (responseMessage.IsSuccessStatusCode)
        {
            return RedirectToAction("Index");
        }

        else{//pass the validation errors in view and display it in view}
    }
}

如何做到这一点?

删除else ,这是没有必要的。

return View(model);

另外,在您的视图中启用不打扰的验证

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM