MVC 2 Beta DefaultControllerFactory 与区域

发布于 2024-08-12 15:16:03 字数 1088 浏览 4 评论 0原文

为什么默认工厂不会返回控制器的全名(带有命名空间)? 我正在使用服务定位器和 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);
        }
}

我有两个家庭控制器(一个位于博客区域下)

http://localhost/Home

http://localhost/Blog/Home

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)

http://localhost/Home

http://localhost/Blog/Home

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手心的温暖 2024-08-19 15:16:03

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.

不再让梦枯萎 2024-08-19 15:16:03

[根据Levi的回答更新]

1.显式注册每个控制器的全名:

 foreach (var tp in currentAssemblyControllersTypes)
                builder.Register(tp).FactoryScoped().Named(tp.FullName)

//Application.Controllers.HomeController
//Application.Areas.Blog.Controllers.HomeController

2.根据PDC09演示,在MapRoute()中为Global.ascx.cs中的Application.Controllers.HomeController指定命名空间

    routes.MapRoute(
                    "Default",                                              // Route name
                    "{controller}/{action}/{id}",                           // URL with parameters
                    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults

                    ,new string [] { "Application.Controllers"}  //Specify namespace

      );

        }

3.重写GetControllerInstance()方法IoCControllerFactory。 CS

Protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, System.Type controllerType)
 {
  return (controllerType == null)? base.GetControllerInstance(requestContext,controllerType):ServiceLocator.Current.GetInstance<IController>(controllerType.FullName);
 }

[Update according to Levi's answer]

1.Register each controller in full name explicitly:

 foreach (var tp in currentAssemblyControllersTypes)
                builder.Register(tp).FactoryScoped().Named(tp.FullName)

//Application.Controllers.HomeController
//Application.Areas.Blog.Controllers.HomeController

2 .Specify namespace in MapRoute() for Application.Controllers.HomeController in Global.ascx.cs according to PDC09 demo

    routes.MapRoute(
                    "Default",                                              // Route name
                    "{controller}/{action}/{id}",                           // URL with parameters
                    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults

                    ,new string [] { "Application.Controllers"}  //Specify namespace

      );

        }

3.Override GetControllerInstance() meothod IoCControllerFactory.cs

Protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, System.Type controllerType)
 {
  return (controllerType == null)? base.GetControllerInstance(requestContext,controllerType):ServiceLocator.Current.GetInstance<IController>(controllerType.FullName);
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文