![](/img/trans.png)
[英].net json deserializer failing for google api returned json result
[英]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.