我在.Net MVC中有一个简单的登录屏幕。在成功登录后,我使用Redirect()方法重定向到给定的url,但是Redirect(url)不会到达任何地方,页面仍然停留在登录屏幕中。
下面的链接获得returnurl fine:@Html.ActionLink("Buy","Login","Account",new { returnUrl = Request.RawUrl })
然后,在登录屏幕中,returnUrl会显示以下url:http://localhost:1820/Account/Login?returnUrl=%2FBasket%2FGoToShopCart
未重定向的重定向(Url):
[HttpPost]
public ActionResult Login(Login l, string returnurl)
{
RegisteredCustomer rc = new RegisteredCustomer();
string url = "";
Repository r = new Repository();
var obj = r.GetUser(l);
if (obj != null)
{
Session["UserID"] = obj.RegisterID.ToString();
url = returnurl;
}
else
url ="/Account/Login";
return Redirect(url);
}`在登录视图中,我将url传递给HttpPost方法:
`@using (Html.BeginForm("Login", "Account", new { returnUrl = Request.QueryString["ReturnUrl"] }, FormMethod.Post))
{
@Html.Hidden("returnUrl", ViewContext.HttpContext.Request.Url.PathAndQuery)
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
...
<input type="submit" value="Enter" />}`
此外,它也不会重定向此url:
http://localhost:1820/Account/Login
我已经检查了这些答案,但无法帮助我:
Redirect to returnURL not working Redirect request to 'ReturnUrl' displays Login Page
发布于 2019-04-26 17:17:57
您可以使用return RedirectToAction("Login","Account");
发布于 2019-04-26 17:25:17
正如我所看到的,url需要被正确地构造。我相信来自https://stackoverflow.com/a/44837491/5770686的@NKosi的答案可以帮助你,也可以使用其他重定向方法,如RedirectToAction,RedirectToRoute等。(https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.controller.redirect?view=aspnet-mvc-5.2)
https://stackoverflow.com/questions/55863904
复制相似问题