MVC 2 Beta DefaultControllerFactory 与区域
为什么默认工厂不会返回控制器的全名(带有命名空间)? 我正在使用服务定位器和 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);
}
}
我有两个家庭控制器(一个位于博客区域下)
controllerName 仅返回“Home”,而没有上述代码中的完整限定名称。 当我尝试注册控制器名称以进行依赖项注入时,这会产生问题。 下面是我现在根据这种情况注册控制器的方法。即使这样也无一例外地打开页面。但是当我访问 http://localhost/Home 时,两个控制器都会被调用。
foreach (var tp in currentAssemblyControllersTypes)
builder.Register(tp).FactoryScoped().Named(tp.Name.Replace("Controller", ""));
有人可以帮忙吗?谢谢。
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DefaultControllerFactory.CreateController() 方法负责返回给定请求的控制器。 controllerName 参数只是路由的 {controller} 部分。 CreateController() 的工作是根据 URL 中指定的控制器名称找出正确的类型,而不是其调用者的工作。
为了使这更容易,DefaultControllerFactory.CreateController() 委托给另外两个方法:GetControllerType() 和 GetControllerInstance()。如果您想使用原始控制器解析逻辑(例如类型查找)但仅更改类型的实例化方式,请按原样保留 CreateController() 和 GetControllerType() 方法,并仅重写 GetControllerInstance()。这已经处理了您正在复制的名称空间查找逻辑,并使您的代码更加简单。
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.
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.
[根据Levi的回答更新]
1.显式注册每个控制器的全名:
2.根据PDC09演示,在MapRoute()中为Global.ascx.cs中的Application.Controllers.HomeController指定命名空间
3.重写GetControllerInstance()方法IoCControllerFactory。 CS
[Update according to Levi's answer]
1.Register each controller in full name explicitly:
2 .Specify namespace in MapRoute() for Application.Controllers.HomeController in Global.ascx.cs according to PDC09 demo
3.Override GetControllerInstance() meothod IoCControllerFactory.cs