【问题标题】:MVC3 Routes with areas... a little confusionMVC3 带区域的路线......有点混乱
【发布时间】:2012-04-10 02:15:18
【问题描述】:

我有一个 MVC3 站点,其中包含 2 个区域和公共区域。我还指定了用于对项目列表进行分页的路线。我的Register_Routes 方法如下所示:

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

        routes.MapRoute(
            "Paginate", // Route name
            "{controller}/Paginate/{itemsPerPage}/{pageNumber}/{searchString}", // URL with parameters
            new { controller = "Home", action = "Index", itemsPerPage = SiteSettings.ItemsPerPage, pageNumber = 1, searchString = UrlParameter.Optional } // Parameter defaults
        );

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

    }

我注意到(但不明白)的是,如果我从主页注销,我的登录页面的重定向看起来像

http://localhost:62695/Account/LogOn?ReturnUrl=%2fHome%2fPaginate

...登录后,我最终进入了我的主页,但 URL 为:

http://localhost:62695/Home/Paginate

在这一点上,我相当肯定我在路线图上搞砸了,但对我来说似乎是对的。我做错了什么?

更新 根据建议,我将路线更改为如下所示:

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

        routes.MapRoute(
            "Paginate", // Route name
            "{controller}/Paginate/{itemsPerPage}/{pageNumber}/{searchString}", // URL with parameters
            new { controller = "Home", action = "Index", searchString = UrlParameter.Optional } // Parameter defaults
        );

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

    }

...主页索引页面似乎确实可以正常工作,但现在分页不能:

        return RedirectToAction("Paginate", new { itemsPerPage = SiteSettings.ItemsPerPage, pageNumber = 1, searchString = string.Empty });

在 Admin\HomeController 中产生 URL:

http://localhost:62695/Admin/Users/Paginate?itemsPerPage=25&pageNumber=1

所以我还是在这里做错了。

更新 2 好的,这就是我让它按照我想要的方式工作的方式: 我的RegisterRoutes 方法现在看起来像这样:

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

        routes.MapRoute(
            null,
            "{area}/{controller}/Paginate/{itemsPerPage}/{pageNumber}/{searchString}", // URL with parameters
            new {area = string.Empty, controller = "Home", action="Paginate", searchString = UrlParameter.Optional } // Parameter defaults
        );

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

...但这不足以解决路由问题。除此之外,我需要将路线添加到我的区域注册中。我的 AdminAreaRegistration 如下所示:

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            null,
            "Admin/{controller}/Paginate/{itemsPerPage}/{pageNumber}/{searchString}", // URL with parameters
            new { controller = "Home", action = "Paginate", searchString = UrlParameter.Optional } // Parameter defaults
        );

        context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }

除了将我的连接更改为 RedirectToRoute 之外,这还使我的 URL 变得漂亮并且可以同时工作。所有答案都有助于实现我的目标,我为所有人 +1 并选择了最接近路径的答案。

【问题讨论】:

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


    【解决方案1】:

    路由按照注册的顺序进行评估。重定向是由您注册的第一个路由生成的,特别是因为您为所有段声明了默认值。您可能需要考虑定义更具体的路线

    更新 使用 RedirectToRoute 而不是 RedirectToAction 来生成所需的 URL

    RedirectToRoute("Paginate", new { itemsPerPage = SiteSettings.ItemsPerPage, pageNumber = 1, searchString = string.Empty });
    

    【讨论】:

    • 好的,如果我删除 itemsPerPagepageNumber 的默认值,我的主页似乎可以正常工作......但我的 RedirectToAction 值现在以标准 GET 格式列出内容(即, http://localhost:62695/Admin/Users/Paginate?itemsPerPage=25&pageNumber=1) 而不是我想要的漂亮漂亮的http://localhost:62695/Admin/Users/Paginate/25/1
    • 您应该改用 RedirectToRoute。我已经更新了上面的答案
    【解决方案2】:
    routes.MapRoute(null, // do not name your routes, it's a "magic string"
        "{controller}/Paginate/{itemsPerPage}/{pageNumber}/{searchString}",
            new 
            { 
                controller = "Home", 
                action = "Index", 
                searchString = UrlParameter.Optional
            } 
        );
    
    // instead of RedirectToAction, try RedirectToRoute, and do not use the route name
    return RedirectToRoute(new 
        { 
            controller = "Home",
            area = "AreaName",
            itemsPerPage = SiteSettings.ItemsPerPage, 
            pageNumber = 1, 
            searchString = string.Empty, 
        }
    );
    

    【讨论】:

    • 这似乎没什么区别。
    • 我忘记了区域路线片段,并更新了我的答案。分页 URL 到底应该映射到什么操作方法? HomeController.Index(int itemsPerPage, int pageNumber, string searchString)? 如果没有,您需要更改您的MapRoute
    【解决方案3】:

    嗯,您返回 URL http://localhost:62695/Home/Paginate 的原因是因为当您登录时返回到指定的 URL,即 http://localhost:62695/Account/LogOn?ReturnUrl=%2fHome%2fPaginate?ReturnUrl=%2fHome%2fPaginate 部分。这不是您主页的网址吗?你从来没有指定过。

    可能是第一个定义也优先,不知道我从哪里听到的,所以如果你把默认定义放在第一位,它可能会抓住它。

    【讨论】:

    • 啊,你看,cecilphillip 也是这么想的。尝试重新排序您的路线定义。
    • hmm...我的主页应该是/,简单明了。所以不要定义默认值...嗯。
    猜你喜欢
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 2012-02-19
    • 1970-01-01
    • 2011-08-12
    相关资源
    最近更新 更多