简体   繁体   中英

Route issue with MVC3 with multiple controller names

here is my navigation hierarchy.

  • Catalog
    -Collection
    - Media
    - Attributes
    - User
  • Collection
    -Media
    - Products
    - Attributes

I have controller for all. This is what my url should look like

http://Localhost/Catalog/Collection/1  // return all collection for catalogeid 1
http://Localhost/Catalog/Media/1  // return all media for catalogeid 1
http://Localhost/Collection/Media/1  // return all media for collectionid 1

Now with default route

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

It looks for action”Collection” in my “Catalog” Controller ,as I have already define “List” method in “Collection” Controller, I don't want to redefine same in my “Collection” Then I tried this

routes.MapRoute(
            "Catalog_Collection_List", // Route name
            "Catalog/Collection/{id}", // URL with parameters
            new { controller = "Collection", action = "List", id = UrlParameter.Optional } // Parameter defaults
        );

But result is the same. Another problem is how to generating link as I mentioned above. I cant use

@Html.ActionLink("Collection","Collection", "List", new { id = 1 })

As this generates link like

Localhost/Collection/List/1   //I don’t want

My ultimate goal is that all the action related to “Collection” should go under “Collection” controller. i dont know what i am missing. hope my question is clear. thanks for any help.

making my question more clear. take a look this sample url

localhost/Catalog/Media/1    // which gives media for catalog
localhost/Collection/Media/1 // which gives media for collection.

now as per conventional way i have to define "Media" action in each controller. which i dont want. what i want is based on URL i want to invoke "List" action from "Media" controller. i hope this is pretty clear.

Im having a bit of a hard time following the problem here. You dont want the url above /collection/list but that is what you specified in your action link. Looking at your URLs you have a consistency problem to start.

"It looks for action”Collection” in my “Catalog” table ,as I have already define method “List” method in “Collection” table, I don't want to redefine same in my “Collection” Then I tried this" by table I think you mean controller here right?

So you should stick to the format in general Url = /Controller/Action/id Collection/List/1.

You can absolutely map it somewhere else (and the order is surely important - the first match in your routing table wins)- but ask yourself if you want to deviate from the standard so far.

EDIT I believe you want this route, add it BEFORE your other routes.

routes.MapRoute(
            "Catalog_Collection_List", // Route name
            "Catalog/Media/{id}", // URL with parameters
            new { controller = "Collection", action = "List", id = UrlParameter.Optional } // Parameter defaults
        );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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