简体   繁体   中英

Response.Redirect not redirecting to a specific URL in ASP.NET MVC

I am trying to redirect to some page after login.

Here is my code

ViewBag.userid = Session[Declaration.sUserID];
var userid = ViewBag.userid;
if (userid == null)
{
    Response.Redirect("signin?url=" + Server.UrlEncode(Request.Url.AbsoluteUri));
}
else
{
    string ReturnUrl = Convert.ToString(Request.QueryString["url"]);
    if (!string.IsNullOrEmpty(ReturnUrl))
    {
        Response.Redirect(ReturnUrl);//http://localhost:55197/usermanagement
    }
    else
    {
        return RedirectToAction("DashBoard");
    }
}
return View();

The problem is that it is not redirecting from Response.Redirect part and always calls the same view again and gain.

Someone have an idea why it's happening?

You need to "return" once you call Response.Redirect, otherwise the code will continue and eventually call return View(), which returns the original view back to the caller. eg

ViewBag.userid = Session[Declaration.sUserID];

    var userid = ViewBag.userid;
    if (userid == null)
    {
        return Redirect("signin?url=" + Server.UrlEncode(Request.Url.AbsoluteUri));
    }
    else
    {
        string ReturnUrl = Convert.ToString(Request.QueryString["url"]);
        if (!string.IsNullOrEmpty(ReturnUrl))
        {
            return Redirect(ReturnUrl);//http://localhost:55197/usermanagement
        }
        else
        {
            return RedirectToAction("DashBoard");
        }
    }


    return View();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM