简体   繁体   中英

MVC QueryString is an empty collection

I want to use a redirection to the requested URL a user wanted to access after the login but somehow the returnUrl is always null. (I did research on that topic for half an hour and didn't find a solution working for me.)

I debugged the Login view of my app and I'm trying to get the ReturnUrl with

new { ReturnUrl = Request.QueryString["ReturnUrl"] }

inside of my @using() tag. I saw that the QueryString is an empty collection and I even tried to use it without QueryString or just with ViewBag.ReturnUrl but neither the one nor the other worked for me.

The controller has the right parameters and it do redirect via RedirectToLocal(returnUrl) but that doesn't matter if the returnUrl is null.

Try this

 public ActionResult FirstAction()
    {
        return RedirectToAction("SecondAction",
            new { test = Request.Url.ToString() });
    }

    public ActionResult SecondAction()
    {
        return Redirect(Request.QueryString["test"]);
    }

In all your action methods,use this code

RedirectToAction("Login", "ControllerName", new { returnUrl = Request.Url.AbsolutePath });

In your Login action method use

public ActionResult Login(LoginViewModel model, string returnUrl){//code}

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