2011-02-10 29 views
3

我試圖讓我的ASP.NET MVC2控制器使用StructureMap構建,但ASP.NET似乎沒有記得我在Global.asax文件中調用了ControllerBuilder.Current.SetControllerFactory設置ASP.NET MVC ControllerFactory沒有任何作用

特別是我得到錯誤,我的控制器沒有無參數的構造函數。堆棧跟蹤顯示我的自定義ControllerFactory從未實際執行。

這裏是我的電話的方法應該告訴ASP.NET其中的ControllerFactory使用方法:

Sub Application_Start() 

    RegisterRoutes(RouteTable.Routes) 

    ControllerBuilder.Current.SetControllerFactory(GetType(StructureMapControllerFactory)) 

    BootStrapper.RegisterDependencies() 

End Sub 

這裏就是我收到異常。

[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 

爲什麼ASP.NET忘記使用哪個ControllerFactory?

回答

2

你的StructureMapControllerFactory是如何定義的?它有一個默認的構造函數嗎?

如果沒有,請嘗試使用SetControllerFactory方法是第二個重載:

// First create the controllerFactory instance... 

ControllerBuilder.Current.SetControllerFactory(controllerFactory); 
+0

您好。我設法通過創建一個新的Visual Studio項目並將代碼從我的其他項目複製到它來解決此問題。一切都在那之後工作。 代碼本身沒有改變。 – 2011-02-17 09:39:57

相關問題