Setting ASP.NET MVC ControllerFactory has no effect

喜欢而已 提交于 2019-12-10 21:55:10

问题


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.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80

Why is ASP.NET forgetting which ControllerFactory to use?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/4952187/setting-asp-net-mvc-controllerfactory-has-no-effect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!