我有一个包含以下代码的 Controller 方法:
ClearAuthenticationCookies();
FormsAuthentication.SetAuthCookie(EmailAddress, false);
RedirectToAction("Index", "Home");
var responseObject = new responseObject
{
responseCode = "2",
responseMessage = "No"
};
return Json(new {responseObject = responseObject});
这是通过 ajax 从另一个网站执行的,但没有在浏览器中显示正确的 url。如果我直接登录,它会起作用:
$.ajax({
type: "POST",
url: "http://localhost:51437/UserAccount/SignOn2",
dataType: 'json',
data: jsonData,
crossDomain: true,
success: function(result) {
},
error: function(result) {
}
});
我还必须点击我的链接两次才能真正让我登录,但找不到该页面。
最佳答案
你的 Controller 签名应该是 public RedirectResult SignOn(...)
并且 return RedirectToAction("Index", "Home");
是你应该返回的对象从你的 Controller 。在此之后不要返回任何 JsonResult
。
更新:
您可以对一个请求返回一个响应。
为了实现你想要的,你有两种选择。首先:返回 json 结果,然后当客户端收到 json 结果并验证它时,您可以向服务器发送另一个请求,然后重定向到另一个页面。或者您可以从客户端重定向,使用 window.location.replace
通过 js 更改当前页面。此外,您可以在服务器上准备链接以实现所有 .net 路由优势并将其包含到 json 结果中,而不是像这样制作:
$.ajax {
..
..
..
success: function(data) {
alert(data.message_for_user);
window.location.replace(data.url_to_redirect);
}
}
关于c# - RedirectToAction 在页面中显示 404,但仍会重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19745672/