0

I have the following LogOn Action [HttpPost]

     public ActionResult LogOn(LogOnModel user, string returnUrl)
        {
  if (!String.IsNullOrEmpty(returnUrl))
                                {
                    return Redirect(returnUrl);
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
}

In which My returnUrl is null.. Can any body tell me .Why my return URL is null

1
  • 1
    No idea, since you don't show your html markup that calls it. Commented Nov 7, 2012 at 6:44

3 Answers 3

2

I wrote up a very fix fix to this. Basically add a route value needs to be added to the Html.BeginForm as follows.

Html.BeginForm("LogOn", 
    "Account", 
    new { returnUrl = Request.QueryString["ReturnUrl"] })

For more details see http://blog.nicholasrogoff.com/2013/12/10/net-mvc-3-logon-does-not-redirect-out-the-boxquick-fix/

Sign up to request clarification or add additional context in comments.

Comments

1

That's depends how you call the controller action method.

  1. If you have an URL like below http://www.somedomain.com/LogOn/LogOn?returnUrl=user/userList

    In this call, your returnUrl parameter of an ActionMethod (LogOn will be replaced by querystring parameter returnUrl.

  2. If you're using form authentication, there is an [Authorize] attribute which validated the authentication. If user is NOT authenticated, then it will redirect to the LogIn page with the querystring parameter returnUrl which will have a requested page url in it.

At this moment, you will also get returnUrl value in controller's action method parameter with returnUrl paramter with value which will have requested page Url

Hope this helps!

2 Comments

I am not have any URL that you have mentioned above.. How to identify the querystring parameter
@Prakash See, Asp.Net MVC uses automatic model binding, which means that whatever you have in your querystring parameter, it will be automatically mapped to your model. If you don't have Such parameter in URL, you will always get NULL in this case because you don't have returnUrl parameter in querystring. Make sense?? Please do let me know.
0

It depends on who is calling this method and whether this parameter is supplied in the POST request.

For example if the user tries to access a controller action decorated with the [Autorize] attribute and he is not authenticated the framework will automatically redirect to the LogOn action (the one that renders the form, not the one with [HttpPost]) and supply the returnUrl parameter.

Then you could have a hidden field in the logon form to persist its value so that when the user enters his credentials and submits the form to the LogOn action, in case of successful login, he is redirected to the initially requested page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.