problem with routing in asp.net mvc3
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开发者_JAVA技巧 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.
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?
}
}
精彩评论