Categories of controllers in MVC routing? (Duplicate controller names in separate namespaces)

I am looking for some examples or routing patterns for the following kind of scenario:

General example of action execution: {controller} / {action} / {id}

So, in the product search script for the store, you will have:

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

Say you had several stores to do this, and you wanted it to be consistent, is there any way: {category} / {controller} / {action} / {id}

So that you can find a specific search for a specific store, but use a different search method for a different store?

(If you want the store name to be a higher priority than the function itself in the URL)

Or it will go down to:

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();
        ...
    }
}

, , , , URL- , , /?

:

, , - , URL-, , .

IE:

/this/search/items/search + term < - works

/that/search/items/search + term < - , .

+3
2

, ASP.NET .

, , , ( , !)

, , "MyWebShop.Controllers", "Shop1" "MyWebShop.Controllers.Shop1"

:

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

        var shop1namespace = new RouteValueDictionary();
        shop1namespace.Add("namespaces", new HashSet<string>(new string[] 
        { 
            "MyWebShop.Controllers.Shop1"
        }));

        routes.Add("Shop1", new Route("Shop1/{controller}/{action}/{id}", new MvcRouteHandler())
        {
            Defaults = new RouteValueDictionary(new
            {
                action = "Index",
                id = (string)null
            }),
            DataTokens = shop1namespace
        });

        var shop2namespace = new RouteValueDictionary();
        shop2namespace.Add("namespaces", new HashSet<string>(new string[] 
        { 
            "MyWebShop.Controllers.Shop2"
        }));

        routes.Add("Shop2", new Route("Shop2/{controller}/{action}/{id}", new MvcRouteHandler())
        {
            Defaults = new RouteValueDictionary(new
            {
                action = "Index",
                id = (string)null
            }),
            DataTokens = shop2namespace
        });

        var defaultnamespace = new RouteValueDictionary();
        defaultnamespace.Add("namespaces", new HashSet<string>(new string[] 
        { 
            "MyWebShop.Controllers"
        }));

        routes.Add("Default", new Route("{controller}/{action}/{id}", new MvcRouteHandler())
        {
            Defaults = new RouteValueDictionary(new { controller = "Home", action = "Index", id = "" }),
            DataTokens = defaultnamespace            
        });
    }

, , , , , , .

+4

- - ControllerFactory, IControllerFactory. CreateController, , RouteHandler ControllerActionInvoker. , . , , . factory , .

, , - . , , . , ViewLocator factory.

, , SO, , - , . .

, , AcceptVerbs. . question. , .

+1

Source: https://habr.com/ru/post/1696669/


All Articles