按区域划分的控制器 [英] Controllers split by areas
问题描述
可能的重复:
如何注册控制器已在 AREA 中创建的
我有一个问题 - 可以做下一个吗?
I have the question - is it possible to do the next?
我有三个领域:
_Default
SiteOne
SiteTwo
在每个区域内,我有一个同名的 ApiController,但当然在不同的命名空间中:
Inside each area i have a ApiController with the same name, but in different namespaces of course:
MvcAppliaction.Areas._Default.Controllers.ValuesController
MvcAppliaction.Areas.SiteOne.Controllers.ValuesController
MvcAppliaction.Areas.SiteTwo.Controllers.ValuesController
我在配置中还有一个当前(我想使用的)区域的值.
I also have a value of current (which i would like to use) Area in configuration.
如果他在浏览器中输入,我想将用户映射到适当区域中的控制器(我可以在配置中找到):
I would like to map user to controller in the proper Area (which i can find in the configuration) if he enters in the browser:
/api/values
例如,如果配置文件中的当前区域是 SiteOne 那么这个请求应该映射到 MvcAppliaction.Areas.SiteOne.Controllers.ValuesController 控制器,但是如果我将配置文件中的当前区域更改为 _Default 它应该映射到正确的控制器.
For example, if current Area in config file is SiteOne then this request should be mapped to MvcAppliaction.Areas.SiteOne.Controllers.ValuesController controller, but if i change current Area in config file to SiteTwo of _Default it should be mapped to correct controller.
附注.使用 MVC 控制器很容易,你只需要设置你的路由:
PS. With MVC controller it's easy, you just have to set your route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MvcApplication.Web.Areas." + SiteName + ".Controllers") }
);
推荐答案
我的建议是实现一个自定义的 DefaultControllerFactory.
My Suggestion would be to implement a custom DefaultControllerFactory.
您可以在此处
默认控制器工厂仅在列表中按名称列出所有控制器,不允许此类功能.上面的文章向您展示了如何创建一个新工厂并控制控制器的创建,使您可以轻松地将路由匹配到特定的命名空间.
The default controller factory just lists all controllers by name on a list not allowing for this kind of functionality. The article above shows you how to create a new factory and take control over the controller creation allowing you to easily match routes to specific namespace's.
这将提供您正在寻找的功能.
That would give the functionality you are looking for.
这篇关于按区域划分的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!