MVC 2 Beta版DefaultControllerFactory与地区 [英] MVC 2 Beta DefaultControllerFactory with Areas
问题描述
为什么出厂默认的不会返回控制器(使用命名空间)的全称是什么?
我使用的服务定位和autofac。
Why default factory WON'T return full name of the controllers (with namespaces)? I'm using Service Locator and autofac.
using System.Web.Mvc;
using Microsoft.Practices.ServiceLocation;
namespace Application.Core.MVC
{
public override IController CreateController(System.Web.Routing.RequestContext requestContext, string **controllerName**)
{
return ServiceLocator.Current.GetInstance<IController>(controllerName);
}
}
我有两个家控制器(一个下面积博客)
I had two home controllers (one under area Blog)
controllerName上面code仅返回家不完全合格的名称为。
当我尝试reactor进行注册控制器的名字依赖注入这就产生了一个问题。
这里是我现在根据这个情况登记控制器。即使这引起了无一例外的网页。但是,当我访问的http://本地主机/主页,两个控制器regardlessly调用。
controllerName return only "Home" without full qualified name for both in above code. This creates a problem when I try to regiser controllers' names for dependency injection. Here is how I register controllers right now according to this situation. Even this brings up the pages without exception. But When I access http://localhost/Home, both controllers invoked regardlessly.
foreach (var tp in currentAssemblyControllersTypes)
builder.Register(tp).FactoryScoped().Named(tp.Name.Replace("Controller", ""));
任何人都可以帮忙吗?谢谢。
Anyone can help?Thanks.
推荐答案
该DefaultControllerFactory.CreateController()方法负责给定请求返回的控制器。该controllerName参数是路由的仅有的{控制器}一部分。它的 CreateController()的的工作 - 而不是它的调用者的 - 找出给定为在URL中指定控制器名称的正确类型
The DefaultControllerFactory.CreateController() method is responsible for returning a controller for the given request. The controllerName parameter is just the {controller} part of the route. It's CreateController()'s job - not its caller's - to figure out the proper type given the controller name as specified in the URL.
为使这更容易,DefaultControllerFactory.CreateController()代表到其他两种方法:GetControllerType()和GetControllerInstance()。如果你想使用原来的控制器解决逻辑(例如型查找),但只是改变类型如何实例化,离开CreateController()和GetControllerType()方法原样,只是重写GetControllerInstance()。这已经需要你复制名称空间查询逻辑的护理,将会让您code就简单得多了。
To make this easier, DefaultControllerFactory.CreateController() delegates into two other methods: GetControllerType() and GetControllerInstance(). If you want to use the original controller resolution logic (e.g. type lookup) but just change how types are instantiated, leave the CreateController() and GetControllerType() methods as-is, and just override GetControllerInstance(). This already takes care of the namespace lookup logic you're duplicating and will make your code far simpler.
这篇关于MVC 2 Beta版DefaultControllerFactory与地区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!