2

Now, when somebody logout- i pass to logout action url, from which page he did it. But the problem, that i cant fire controller's action of url, where he pressed exit.

For example, user was on page

site.com/cabinet/home/index

and here pressed url like:

site.com/Account/LogOut/?returnURL=/cabinet/home/index

public ActionResult LogOut(string returnURL)
{
    // Need to RedirectToAction(...)
    return Redirect(returnURL);
}

So, if i redirect him to returnURL- nothing gonna happen in my controller (even not in controller,- in View, which gonna be redrawen for unathorized user (not the same interface for auth and not auth user)).

So, should i extract controller, action, area (?), params and only after this make RedirectToAction? Or any other ways to make controller be fired, when u know only url?

Thx.

6
  • you should to return to the URL only if loggin was done, isn't it? Commented Sep 19, 2012 at 13:46
  • hm, dont take attantion on anything else that identify from url controller, action, area and parameters. Task: get url => get values for RedirectToAction or any other ways to fire action in controller Commented Sep 19, 2012 at 13:49
  • Why do you need to extract the Controller and Action? You should be able to redirect to a string like "/cabinet/home/index". That's exactly what happens in the Login Action of the default AccountController in an ASP.NET MVC Internet Application. Commented Sep 19, 2012 at 14:00
  • But it doesn't fire action in controller, so i can't identify if user is authorized and redraw for him view. Commented Sep 19, 2012 at 14:06
  • When you do return Redirect("/cabinet/home/index"); it doesn't trigger the Index action of the Home controller in the Cabinet area? What does the browser show you? Maybe it's a caching problem? Commented Sep 19, 2012 at 17:42

2 Answers 2

0

in your View:

@using (Html.BeginForm("LogOut", "Account", 
    new { returnUrl = HttpContext.Current.Request.Url.LocalPath },
    FormMethod.Post))
{
<input type="submit" name="logout" value="Выход" />
}

in Action:

    public ActionResult LogOut(string returnUrl)
    {
        FormsAuthentication.SignOut();
        if (Url.IsLocalUrl(returnUrl))
        {
          //  return RedirectToAction("LogIn", "Account", new { returnUrl = returnUrl });
            return Redirect(returnUrl);
        }
        else
        {

            return Redirect(FormsAuthentication.LoginUrl);
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

-3
    return RedirectToAction(ActionName, ControllerName)

5 Comments

Yes. There are several ways to redirect, each with its own subtle differences. I often have to try 2 or 3 ways before I get what I am after. Experiment with intellisense.
Man, what are u talking about? I need to identify controller, action, area, params from passed to controller url. Look even at title of question.
So is your URL arbitrary? I hope not! An MVC3 URL contains the controller and action. If redirect is not working, then either you need one of the other redirect methods, or your URL is malformed.
Yeap, i need other redirect method. But how i extract controller name, action name, area name and passes parameters for this 'other redirect method' if i get only string url parameter? This is the question of this topic.
You can use additional query strings, like "site.com/Account/LogOut/?returnURL=/cabinet/home/index&areaName=myArea" and change the signature of the method to "public ActionResult LogOut(string returnURL, string areaName), or you can expand the url and parse it in the method, or you can expand the url and use routes, but I haven't needed to do that, so can't really explain it.

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.