【问题标题】:Controller that returns JSON format not returning to target page返回 JSON 格式的控制器不返回目标页面
【发布时间】:2021-05-14 18:09:46
【问题描述】:
-
为什么我的控制器登录后无法进入目标索引页面?
[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult Login(FormCollection fc)
{
var isSuccess = false;
Array errors = null;
more code here...
return Json(new { infoMessage = errors, successMessage = "", Status = isSuccess, gotoUrl = string.Format("/MyAccounts/Index") }, JsonRequestBehavior.AllowGet);
}
动机是将页面重定向到索引,但它返回的是 Json 结果文档。
【问题讨论】:
标签:
model-view-controller
jsonresult
【解决方案1】:
在if语句中添加return
if(isSuccess == true) {
//return redirect to target page
}
else {
return Json(new { infoMessage = errors, successMessage = "", Status = isSuccess, gotoUrl = string.Format("/MyAccounts/Index") }, JsonRequestBehavior.AllowGet);
}
【解决方案2】:
它不会转到任何目标页面,因为您指定它返回 json。你想要的是重定向到一个动作。
return RedirectToAction("Index", "MyAccounts");