开发者

Setting ASP.NET MVC ControllerFactory has no effect

开发者 https://www.devze.com 2023-02-09 20:35 出处:网络
I\'m trying to have my ASP.NET MVC2 controllers built using StructureMap but ASP.NET doesn\'t seem to remember that I\'ve called ControllerBuilder.Current.SetControllerFactory in my Global.asax file.

I'm trying to have my ASP.NET MVC2 controllers built using StructureMap but ASP.NET doesn't seem to remember that I've called ControllerBuilder.Current.SetControllerFactory in my Global.asax file.

Specifically I get the error that my controller has no parameterless constructor. The stack trace reveals that my custom ControllerFactory was never actually executed.

Here is my call to the method that should tell ASP.NET which ControllerFactory to use:

Sub Application_Start()

    RegisterRoutes(RouteTable.Routes)

    ControllerBuilder.Current.SetControllerFactory(GetType(StructureMapControllerFactory))

    BootStrapper.RegisterDependencies()

End Sub

And here's the exception that I receive.

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
  System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck) +0
  System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache) +86
  System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache) +230
  System.Activator.CreateInstance(Type type, Boolean nonPublic) +67
  System.Web.Mvc.DefaultControll开发者_StackOverflow中文版erFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80

Why is ASP.NET forgetting which ControllerFactory to use?


How is your StructureMapControllerFactory defined? Does it have a default constructor?

If not, try using that second overload of the SetControllerFactory method:

// First create the controllerFactory instance...

ControllerBuilder.Current.SetControllerFactory(controllerFactory);


You have to set your controllerFactory before you RegisterRoutes...

0

精彩评论

暂无评论...
验证码 换一张
取 消