在url上重用route参数

时间:2010-06-30 14:54:07

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

如何将我的应用程序路由到mydomainname / username / controller。

我正在开发asp.net mvc web应用程序,使用户可以属于多个帐户。即。在应用程序中,每个帐户都有自己的用户,一个帐户中的每个用户也可以是另一个帐户中的用户。我需要的是当用户想要登录时,他们指定他们想要登录的帐户,如:domainname.com/accountname/login。

能够做到这一点,但问题是如何在其他路线上保留accountname route参数?我的意思是让它在网址上可见。现在我正在使用cookie来存储和获取accountname参数,但是我需要一种方法让它在每个请求中的url上都可见(无需在链接上手动路由它),直到用户唱出。

使用asp.net mvc 2

修改:添加了我的路线代码

        public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute("", "", new { controller = "Home", action = "index" });

        routes.MapRoute("", "dashboard", new { controller = "account", action = "dashboard" });

        routes.MapRoute("", "contacts", new { controller = "contact", action = "index" });

        routes.MapRoute("", "groups", new { controller = "group", action = "index" });

        routes.MapRoute("", "sms", new { controller = "sms", action = "index" });

        routes.MapRoute("", "users", new { controller = "user", action = "index" });

        routes.MapRoute("", "login", new { controller = "Home", action = "login", accountUrlName = UrlParameter.Optional });

        routes.MapRoute("", "{accountUrlName}/login", new { controller = "Home", action = "login" });

        routes.MapRoute("", "register", new { controller = "home", action = "register" });

        routes.MapRoute("", "{accountUrlName}/invitations/{ivkey}", new { controller = "home", action = "invitations" });

        routes.MapRoute("", "{urlName}",
            new { controller = "home", action = "index", urlName = UrlParameter.Optional });

        routes.MapRoute("", "{accountUrlName}/{action}",
           new { controller = "account", action = "dashboard", id = "", accountUrlName = UrlParameter.Optional });

        routes.MapRoute("", "{accountUrlName}/{controller}/{action}/{id}",
            new { controller = "account", action = "dashboard", id = "", accountUrlName = ""});

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

    }

1 个答案:

答案 0 :(得分:0)

所以你在这里有效地拥有两套路线 -

{AccountName}/{Controller}/{Action}{Username}/{Controller}/{Action}

这是对的吗?

您可以创建这些路线,但是您必须拥有不包含帐户名称的用户名,反之亦然。