asp.net-mvc - MVC 路由中 Controller 的类别? (不同命名空间中的重复 Controller 名称)

标签 asp.net-mvc routes controller

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

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

因此,在对商店进行产品搜索的情况下,您将拥有:

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

假设您有几家商店可以这样做,并且您希望始终如一地这样做,有什么办法可以拥有:{category}/{controller}/{action}/{id}

这样您就可以对特定商店进行特定搜索,但对不同商店使用不同的搜索方法?

(如果您要求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();
        ...
    }
}

这可能不是一个很好的例子,但基本上的想法是使用相同的 Controller 名称,因此在几个不同的场景中都有一个简单的 URL,或者您是否坚持要求唯一的 Controller 名称,并且没有办法将它们放在稍微不同的命名空间/目录中?

编辑添加:

我想要这个的另一个原因是因为我可能想要一个包含类别的网址,并且某些 Controller 只能在某些类别下工作。

IE:

/this/search/items/search+term <-- 有效

/that/search/items/search+term <-- 不起作用 - 因为不允许使用搜索 Controller 。

最佳答案

我实际上不是通过搜索找到的,而是通过扫描 this question 中的 ASP .NET 论坛找到的。 .

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

但是从这里开始,您可以在 Controller 下放置一个目录,因此如果您的 Controller 是“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            
        });
    }

唯一的另一件事是,它将引用仍在基目录中的 View ,因此,如果您将 View 放入匹配的目录中,则当您将其返回到 Controller 内时,必须将 View 名称放入其中。

关于asp.net-mvc - MVC 路由中 Controller 的类别? (不同命名空间中的重复 Controller 名称),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43201/

相关文章:

asp.net - 用于将 ASP.NET MVC 6 网站部署到 Azure 的脚本

linux - Ubuntu 14.04 忽略内核路由

ruby-on-rails - RSPEC 错误 : expected: [2015-05-01 19:56:25 -0700] got: nil (compared using ==) Ruby on Rails

android - android中两个标记之间的路由

ruby-on-rails - 如何为 ApplicationController 中的 after_action 过滤器中的所有操作呈现 json?

javascript - 解析后的 AngularJS 负载 Controller 不起作用

asp.net-mvc - 使用 MVC 时,如何调用 Controller Action 并传递文本框值?

jquery - 自动完成应用值而不是标签到文本框

javascript - 页面重新加载后维护菜单的最佳实践是什么?我有这个自定义代码,感觉不对

asp.net - 路由 HTTP 错误 404.0 0x80070002