3

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

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

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

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

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}

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

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

Or would it come 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();
        ...
    }
}

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?

Edit to add:

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.

IE:

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

/that/search/items/search+term <-- won't work - because the search controller isn't allowed.

4

2 回答 2

3

实际上,我什至没有通过搜索找到它,而是通过扫描这个问题中的 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            
        });
    }

唯一的另一件事是它将引用仍在基目录中的视图,因此如果您将视图放入目录以匹配,当您将视图名称返回到控制器中时,您必须将视图名称放入其中。

于 2008-09-05T01:30:27.807 回答
1

在不妥协的情况下做到这一点的最佳方法是通过继承 IControllerFactory 来实现您自己的 ControllerFactory。您将实现的 CreateController 方法处理创建控制器实例以处理 RouteHandler 和 ControllerActionInvoker 的请求。约定是在创建控制器时使用控制器的名称,因此您需要覆盖此功能。这将是您放置基于路由创建控制器的自定义逻辑的位置,因为您将拥有多个具有相同名称但位于不同文件夹中的控制器。然后你需要在应用程序启动时注册你的自定义控制器工厂,就像你的路由一样。

您需要考虑的另一个领域是在创建控制器时找到您的视图。如果您打算对所有这些视图使用相同的视图,那么您不必做任何与所使用的约定不同的事情。如果您还计划组织视图,那么您还需要创建自己的 ViewLocator 并在控制器工厂中创建它时将其分配给控制器。

为了了解代码,我在 SO 上回答了一些与此问题相关的问题,但这个问题在某种程度上有所不同,因为控制器名称是相同的。我提供了参考链接。

另一种可能需要妥协的方法是使用新的 AcceptVerbs 属性。查看此问题以获取更多详细信息。我还没有玩过这个新功能,但它可能是另一条路线。

于 2008-09-04T12:24:28.180 回答