gpt4 book ai didi

asp.net-mvc-4 - ASP :NET MVC4 url routing for controllers with the same name in different namespaces

转载 作者:行者123 更新时间:2023-12-02 07:36:56 26 4
gpt4 key购买 nike



在我的 Asp.NET MVC4 web 项目中,我有很多 Controller ,我想将它们放入文件夹“Controllers”下的子目录中。有的controller因为主要的工作会有相同的名字,但是会放在不同的子目录下,也就是不同的命名空间。我想为这些 Controller 做一个 url 路由。如何让路由考虑存在同名 Controller 的命名空间?你能展示一些代码如何在 Global.asax 文件中做到这一点吗?
非常感谢。

最佳答案

您需要多个 MapRoute-s - 每个命名空间至少一个。

 // match all controllers in first namespace
routes.MapRoute(
name: "Namespace1Routes",
url: "path1/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "My.Namespace1" }
);

// match all controllers in second and third namespaces
routes.MapRoute(
name: "Namespace2Routes",
url: "path2/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "My.Namespace2", "My.Namespace3" }
);

关于asp.net-mvc-4 - ASP :NET MVC4 url routing for controllers with the same name in different namespaces,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15386196/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com