我将登录信息从jQuery AJAX调用发送到MVC 4控制器:
$.post(url, data, function (response) {
if (response=='InvalidLogin') {
//show invalid login
}
else if (response == 'Error') {
//show error
}
else {
//redirecting to main page from here for the time being.
window.location.replace("http://localhost:1378/Dashboard/Index");
}
});
如果登录成功,我想根据用户类型将用户从服务器端重定向到适当的页面。如果登录失败,则会将字符串发送回用户:
[HttpPost]
public ActionResult Index(LoginModel loginData)
{
if (login fails)
{
return Json("InvalidLogin", JsonRequestBehavior.AllowGet);
}
else
{
// I want to redirect to another controller, view, or action depending
// on user type.
}
}
但是有问题:
如果此方法返回“ ActionResult”,那么我将收到错误消息not all code paths return a value。
如果我使用“ void”,则无法返回任何内容。
即使我使用“ void”且没有返回值,由于jQuery AJAX调用的异步特性,我也无法重定向到其他控制器或视图。
有什么技术可以处理这种情况?