0

I have a page under the default /Home/Index. On this page I have a link to log out the user:

@Html.ActionLink("LogOut", "LogOut", new { controller="Users" })

When I click on this link, debugger goes to default controller, that means Home and action Index. This is my routing

routes.MapRoute(
         "LogOut", // Route name
         "Users/LogOut", // URL with parameters
         new { controller = "Users", action = "LogOut" }
         );

routes.MapRoute(
         "Default", // Route name
         "{controller}/{action}", // URL with parameters
         new { controller = "Home", action = "Index" } // Parameter defaults
         );

What is wrong here? Why it isn't going to the appropriate controller and action?

[EDIT] From js code I can log out the user using

$.post('/Users/LogOut',function(){
window.location.replace("/Home/Index");
});

My LogOut action is simple

public ActionResult LogOut()
        {
            string login = HttpContext.User.Identity.Name;    
            usersService.RemoveLogin(login);
            usersService.RemoveUsersMessages(login);    
            System.Web.Security.FormsAuthentication.SignOut();
            return RedirectToActionPermanent("LogIn", "Users");
        }

but the problem is not with this method. When I click on the link I'm going streight to the default default /Home/Index. Don't know why from client code it works, but using link to send postback to server not.

5
  • 2
    Looks like both routes are the same. Commented Jul 6, 2011 at 10:05
  • So can you show me how to fix it? I have problem to see, where the problem is. Commented Jul 6, 2011 at 10:08
  • What else routes do you have? Commented Jul 6, 2011 at 10:09
  • That's all. Only these two. But I have found something. If I clean the cache in browser and log to my app, then when I click on this link, I'm going to the LogOut action. But if I log myself from this vbrowser once again and click the link, debugger goes to the /Home/Index. That situation I have on IE, Opera and Firefox. Only in Chrome no matter how many times I log in, link works ok. This is strange situation which I can't understand. Commented Jul 6, 2011 at 10:15
  • It seems to be something is being cached, are you caching anything? Did you check the FormsAuthentication in web.config? Commented Jul 6, 2011 at 10:35

1 Answer 1

1

1. Omit the first route definition

Because it maps to the same thing as your default route defined after it:

controller = Users
action = LogOut

So this would still be the same if there was no custom Users/LogOut route...

2. Define your ActionLink properly

You don't have to define controller as you did (although it should work just as well).

@Html.ActionLink("LogOut", "LogOut", "Users")

should do the trick just fine.

3. If this still doesn't do the trick

Then provide the code (edit your question) you have in:

public class UsersController
{
    function ActionResult LogOut()
    {
        // what's inside here?
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I have used workaround for this, but after a month, I still want to know why this isn't working. Even if I remove the first route from my prevoius post and change the action link according to your suggestion, this doesn't work. What is strange is that, from js or using a form on the page I can send request to this address and it work like the link should. I will update my previous post with my code.

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.