MVC中控制器类别路由? (单独的命名空间中的重复控制器名称) [英] Categories of controllers in MVC Routing? (Duplicate Controller names in separate Namespaces)

查看:132
本文介绍了MVC中控制器类别路由? (单独的命名空间中的重复控制器名称)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找以下类型的路由的一些示例或示例:

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

做事的一般示例是:{controller} / {action } / {id}

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
    { ... }
}

假设你有几家商店做这件事,方法:{category} / {controller} / {action} / {id}

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)

或者它会下来:

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,或者你是那种卡住需要唯一的控制器名称,没有办法把它们放在稍微不同的命名空间/目录?

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?

编辑添加:

我想要的另一个原因是因为我可能需要一个url

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

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

/ that / search / items / search + term< - 无法工作 - 不允许。

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

推荐答案

我实际上发现它甚至不是通过搜索,而是通过扫描ASP.NET论坛< a href =http://forums.asp.net/t/1296928.aspx?PageIndex=1 =nofollow noreferrer>此问题。

I actually found it not even by searching, but by scanning through the ASP .NET forums in this question.

使用它,你可以在命名空间的任何部分下拥有相同名称的控制器,只要你限定哪些路由属于哪些命名空间(如果你需要,每个路由可以有多个命名空间!)

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

但是从这里,你可以放在你的控制器下的一个目录,所以如果你的控制器是MyWebShop.Controllers,你会放一个Shop1目录,命名空间beMyWebShop.Controllers.Shop1

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"

然后这个工作原理:

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

唯一的另一件事是,它将引用一个视图, ,所以如果你把视图放到目录匹配,你将不得不把视图名称当你返回控制器里面。

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.

这篇关于MVC中控制器类别路由? (单独的命名空间中的重复控制器名称)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆