[英]display ap.net web api returned result to mvc view
I creating a web application using mvc project and web api separate projects I am using identity for registering users. 我使用mvc项目和Web api单独的项目创建Web应用程序,我使用身份来注册用户。 My question is when the web api returns the result how can I display it in MVC view
我的问题是当Web API返回结果时,如何在MVC视图中显示结果
this is my web api controller - 这是我的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);
}
}
AND THIS is how i am calling this web api - 这就是我如何称呼此网络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}
}
}
how to achieve this? 如何做到这一点?
remove else
, it's not necessary. 删除
else
,这是没有必要的。
Add 加
return View(model);
Also, enable unobtrusive validation in your view 另外,在您的视图中启用不打扰的验证
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.