简体   繁体   English

带区域的 MVC3 路由……有点混乱

[英]MVC3 Routes with areas... a little confusion

I have an MVC3 site with 2 areas plus the common area.我有一个包含 2 个区域和公共区域的 MVC3 站点。 I also have a route specified for paginating lists of items.我还指定了一条用于对项目列表进行分页的路线。 My Register_Routes method looks like so:我的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
        );

    }

What I've noticed (and don't understand) is if I log out from my home page, the redirect from my login page looks like我注意到(但不明白)的是,如果我从主页注销,登录页面的重定向看起来像

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

... and upon logging in, I end up on my home page, except with a URL of: ......登录后,我最终进入了我的主页,但 URL 除外:

http://localhost:62695/Home/Paginate

I'm fairly certain at this point that I've screwed something up with the route map, but it seems right to me.在这一点上,我相当确定我在路由 map 上搞砸了,但这对我来说似乎是对的。 What am I doing wrong?我究竟做错了什么?

UPDATE per suggestion, I changed my routes to look like this:根据建议更新,我将路线更改为如下所示:

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

    }

... and the home index pages do indeed seem to work properly, but now the paginates don't: ...并且主页索引页面确实看起来确实可以正常工作,但现在分页不:

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

in a Admin\HomeController yields the URL:在 Admin\HomeController 中产生 URL:

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

so I'm still doing something wrong here.所以我仍然在这里做错事。

UPDATE 2 OK, this is how I got it to work the way I wanted it to: My RegisterRoutes method now looks like this:更新 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
        );
    }

... but this was not enough to fix the routing issues. ...但这还不足以解决路由问题。 Additionally to this, I needed to add the route to my area registrations.除此之外,我需要将路线添加到我的区域注册中。 My AdminAreaRegistration looks like this:我的 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 }
        );
    }

This, in addition to changing to RedirectToRoute for my connections, made my URLs all pretty and working at the same time.这除了为我的连接更改为RedirectToRoute之外,还使我的 URL 变得漂亮并且同时工作。 All the answers helped to get to my goal, I +1'd everyone and chose the answer that got me closest to the path.所有的答案都有助于实现我的目标,我为每个人 +1,并选择了让我最接近路径的答案。

Routes are evaluated in the order they are registered.路由按照它们注册的顺序进行评估。 The redirects are being generated by the first route that you registered, especially since you declared default values for all of the segments.重定向是由您注册的第一条路线生成的,特别是因为您为所有段声明了默认值。 You might want to consider defining a more specific route您可能需要考虑定义更具体的路线

UPDATE Use RedirectToRoute instead of RedirectToAction to get your desired URL generation更新使用 RedirectToRoute 而不是 RedirectToAction 来获得所需的 URL 代

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

Well, the reason you return to the URL http://localhost:62695/Home/Paginate is because when you log in you return to the URL specified, namely the ?ReturnUrl=%2fHome%2fPaginate part of http://localhost:62695/Account/LogOn?ReturnUrl=%2fHome%2fPaginate .那么,您返回到 URL http://localhost:62695/Home/Paginate的原因是因为当您登录时,您返回到指定的 URL,即 http://localhost 的?ReturnUrl=%2fHome%2fPaginate部分http://localhost:62695/Account/LogOn?ReturnUrl=%2fHome%2fPaginate Is that not the URL of your home page?那不是你主页的URL吗? You never specified.你从来没有指定。

It could be that the first definition takes precedence also, not sure where I heard that, so maybe if you put the default definition first it will grab that.可能第一个定义也优先,不知道我在哪里听说过,所以如果你把默认定义放在第一位,它可能会抓住它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM