<p>I'm looking for some examples or samples of routing for the following sort of scenario:</p>

<p>The general example of doing things is: {controller}/{action}/{id}</p>

<p>So in the scenario of doing a product search for a store you'd have:</p>

<pre><code>public class ProductsController: Controller
{
    public ActionResult Search(string id) // id being the search string
    { ... }
}
</code></pre>

<p>Say you had a few stores to do this and you wanted that consistently, is there any way to then have: {category}/{controller}/{action}/{id}</p>

<p>So that you could have a particular search for a particular store, but use a different search method for a different store?</p>

<p>(If you required the store name to be a higher priority than the function itself in the url)</p>

<p>Or would it come down to:</p>

<pre><code>public class ProductsController: Controller
{
    public ActionResult Search(int category, string id) // id being the search string
    { 
        if(category == 1) return Category1Search();
        if(category == 2) return Category2Search();
        ...
    }
}
</code></pre>

<p>It may not be a great example, but basically the idea is to use the same controller name and therefore have a simple URL across a few different scenarios, or are you kind of stuck with requiring unique controller names, and no way to put them in slightly different namespaces/directories?</p>

<p>Edit to add:</p>

<p>The other reason I want this is because I might want a url that has the categories, and that certain controllers will only work under certain categories.</p>

<p>IE:</p>

<p>/this/search/items/search+term  &lt;-- works</p>

<p>/that/search/items/search+term  &lt;-- won't work - because the search controller isn't allowed.</p>

[{'answer_id': 43623, 'author': 'Dale Ragan', 'author_id': 1117, 'author_profile': 'https://Stackoverflow.com/users/1117', 'pm_score': 1, 'selected': False, 'text': '<p>The best way to do this without any compromises would be to implement your own ControllerFactory by inheriting off of IControllerFactory.  The CreateController method that you will implement handles creating the controller instance to handle the request by the RouteHandler and the ControllerActionInvoker.  The convention is to use the name of the controller, when creating it, therefore you will need to override this functionality.  This will be where you put your custom logic for creating the controller based on the route since you will have multiple controllers with the same name, but in different folders.  Then you will need to register your custom controller factory in the application startup, just like your routes.</p>\n\n<p>Another area you will need to take into consideration is finding your views when creating the controller.  If you plan on using the same view for all of them, then you shouldn\'t have to do anything different than the convention being used.  If you plan on organizing your views also, then you will need to create your own ViewLocator also and assign it to the controller when creating it in your controller factory.</p>\n\n<p>To get an idea of code, there are a few questions I have answered on SO that relate to this question, but this one is different to some degree, because the controller names will be the same.  I included links for reference.</p>\n\n<ul>\n<li><a href="https://stackoverflow.com/questions/19746/views-in-seperate-assemblies-in-aspnet-mvc">Views in separate assemblies in ASP.NET MVC</a></li>\n<li><a href="https://stackoverflow.com/questions/26715/aspnet-mvc-subfolders">asp.net mvc - subfolders</a></li>\n</ul>\n\n<p>Another route, but may require some compromises will be to use the new AcceptVerbs attribute.  Check this <a href="https://stackoverflow.com/questions/36197/aspnet-mvc-structuring-controllers">question</a> out for more details.  I haven\'t played with this new functionality yet, but it could be another route.</p>\n'}, {'answer_id': 45076, 'author': 'crucible', 'author_id': 3717, 'author_profile': 'https://Stackoverflow.com/users/3717', 'pm_score': 2, 'selected': False, 'text': '<p>I actually found it not even by searching, but by scanning through the ASP .NET forums in <a href="http://forums.asp.net/t/1296928.aspx?PageIndex=1" rel="nofollow noreferrer">this question</a>.</p>\n\n<p>Using this you can have the controllers of the same name under any part of the namespace, so long as you qualify which routes belong to which namespaces (you can have multiple namespaces per routes if you need be!)</p>\n\n<p>But from here, you can put in a directory under your controller, so if your controller was "MyWebShop.Controllers", you\'d put a directory of "Shop1" and the namespace would be "MyWebShop.Controllers.Shop1"</p>\n\n<p>Then this works:</p>\n\n<pre><code>    public static void RegisterRoutes(RouteCollection routes)\n    {\n        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");\n\n        var shop1namespace = new RouteValueDictionary();\n        shop1namespace.Add("namespaces", new HashSet&lt;string&gt;(new string[] \n        { \n            "MyWebShop.Controllers.Shop1"\n        }));\n\n        routes.Add("Shop1", new Route("Shop1/{controller}/{action}/{id}", new MvcRouteHandler())\n        {\n            Defaults = new RouteValueDictionary(new\n            {\n                action = "Index",\n                id = (string)null\n            }),\n            DataTokens = shop1namespace\n        });\n\n        var shop2namespace = new RouteValueDictionary();\n        shop2namespace.Add("namespaces", new HashSet&lt;string&gt;(new string[] \n        { \n            "MyWebShop.Controllers.Shop2"\n        }));\n\n        routes.Add("Shop2", new Route("Shop2/{controller}/{action}/{id}", new MvcRouteHandler())\n        {\n            Defaults = new RouteValueDictionary(new\n            {\n                action = "Index",\n                id = (string)null\n            }),\n            DataTokens = shop2namespace\n        });\n\n        var defaultnamespace = new RouteValueDictionary();\n        defaultnamespace.Add("namespaces", new HashSet&lt;string&gt;(new string[] \n        { \n            "MyWebShop.Controllers"\n        }));\n\n        routes.Add("Default", new Route("{controller}/{action}/{id}", new MvcRouteHandler())\n        {\n            Defaults = new RouteValueDictionary(new { controller = "Home", action = "Index", id = "" }),\n            DataTokens = defaultnamespace            \n        });\n    }\n</code></pre>\n\n<p>The only other thing is that it will reference a view still in the base directory, so if you put the view into directories to match, you will have to put the view name in when you return it inside the controller.</p>\n'}]
['https://Stackoverflow.com/questions/43201', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3717/']