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

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

问题描述

我在寻找以下那种情景路由的一些例子或样本:

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

做事的一般例子是:{控制器} / {行动} / {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
    { ... }
}

假设你有一些商店做到这一点,你想,始终是有什么办法则有:{类别} / {控制器} / {行动} / {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?

或将它归结为:

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?

编辑补充:

其他的原因,我想这是因为我可能要具有类网址,以及某些控制器将只在某些类别的工作。

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:

/本/搜索/项目/搜索+术语LT; - 工程

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

/是/搜索/项目/搜索+术语LT; - 将无法正常工作 - 因为搜索器是不允许的。

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

推荐答案

其实,我发现它甚至没有通过搜索,而是通过在的这个问题

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的目录和命名空间将是MyWebShop.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天全站免登陆