按地区划分控制器 [英] Controllers split by areas

查看:286
本文介绍了按地区划分控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/9485934/how-do-i-register-a-controller-that-has-been-created-in-an-area\">How我注册的是在一个领域已经创建

控制器

我有这个问题 - 是有可能做下一个

我有三个方面:

  _Default
SiteOne
SiteTwo

在每个领域我有相同名称的ApiController,但当然不同的命名空间:

  MvcAppliaction.Areas._Default.Controllers.Values​​Controller
MvcAppliaction.Areas.SiteOne.Controllers.Values​​Controller
MvcAppliaction.Areas.SiteTwo.Controllers.Values​​Controller

我也有配置电流(我想用)区的值。

我想映射用户在正确的区域(我可以在配置中找到)连接到控制器,如果他在浏览器中输入:

  / API /值

例如,如果在配置文件中当前区域SiteOne那么这个请求应该被映射到控制器MvcAppliaction.Areas.SiteOne.Controllers.Values​​Controller,但如果我在配置文件中的_Default改变SiteTwo当前区域就应该被映射到正确的控制器。

PS。随着MVC控制器,它很容易,你只需要设置您的路线:

  routes.MapRoute(
    名称:默认,
    网址:{控制器} / {行动} / {ID}
    默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional},
    命名空间:新[] {MvcApplication.Web.Areas。 +网站名称+.Controllers)}
);


解决方案

我的建议是实现一个自定义DefaultControllerFactory。

您可以看到一个很好的例子这里

默认的控制器工厂只是列出不允许对这类功能的名单上的名字所有控制器。上面的文章向您展示如何创建一个新的工厂,并采取控制了控制器创建,让您轻松匹配的路由特定的命名空间的。

这会给你正在寻找的功能。

Possible Duplicate:
How do I register a controller that has been created in an AREA

I have the question - is it possible to do the next?

I have three Areas:

_Default
SiteOne
SiteTwo

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

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.

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

解决方案

My Suggestion would be to implement a custom DefaultControllerFactory.

You can see a very good example here

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

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