在asp.net mvc3中路由的问题

时间:2011-07-06 09:52:25

标签: .net asp.net-mvc-3 routing

我在默认/ Home / Index下有一个页面。在这个页面上,我有一个注销用户的链接:

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

当我点击此链接时,调试器进入默认控制器,这意味着Home和action Index。 这是我的路由

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
         );

这里有什么问题?为什么没有采用适当的控制器和动作?

[编辑] 从js代码我可以使用

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

我的LogOut操作很简单

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

但问题不在于此方法。当我点击链接时,我将直接进入默认的默认/ Home / Index。不知道为什么从它的客户端代码工作,但使用链接发送回服务器不。

1 个答案:

答案 0 :(得分:1)

1。省略第一个路径定义

因为它映射到与之后定义的默认路由相同的东西:

controller = Users
action = LogOut

如果没有自定义Users/LogOut路线......

,这仍然是相同的

2。正确定义ActionLink

您不必像以前那样定义控制器(尽管它也可以正常工作)。

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

应该做的很好。

3。如果这仍然不起作用

然后提供您的代码(编辑您的问题):

public class UsersController
{
    function ActionResult LogOut()
    {
        // what's inside here?
    }
}