2015-11-03 61 views
1

我使用的是Unity,即使我不應該註冊我的控制器(據我所知),我的AccountController上出現以下錯誤,它只是一個在製作新項目時從默認MVC模板中獲取。AccountController上的ASP MVC ControllerFactory失敗

The IControllerFactory 'WebApplication1.Models.ControllerFactory' did not return a controller for the name 'Account'. 

堆棧跟蹤:

[InvalidOperationException: The IControllerFactory 'WebApplication1.Models.ControllerFactory' did not return a controller for the name 'Account'.] 
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +336 
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +50 
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48 
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16 
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +103 
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155 

的的ControllerFactory看起來是這樣的:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using System.Web.Routing; 
using Microsoft.Practices.Unity; 

namespace WebApplication1.Models 
{ 
public class ControllerFactory : DefaultControllerFactory 
{ 
    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) 
    { 
     try 
     { 
      if (controllerType == null) 
      { 
       throw new ArgumentNullException("controllerType"); 
      } 
      if (!typeof(IController).IsAssignableFrom(controllerType)) 
      { 
       throw new ArgumentException(string.Format("Type requested is not a controller: {0}", controllerType.Name), "controllerType"); 
      } 
      return MvcUnityContainer.Container.Resolve(controllerType) as IController; //This is where it fails 
     } 
     catch (Exception e) 
     { 
      return null; 

     } 
    } 
} 

public static class MvcUnityContainer 
{ 
    public static UnityContainer Container { get; set; } 
} 
} 

捕獲到的異常給了我這樣的:

e = {"Resolution of the dependency failed, type = \"WebApplication1.Controllers.AccountController\", name = \"(none)\".\r\nException occurred while: while resolving.\r\nException is: InvalidOperationException - The current type, Microsoft.AspNet.Identity.IUserS... 

我不知道在哪裏從這裏出發,並有任何幫助讚賞。

UPDATE:

AccountController類:

[Authorize] 
public class AccountController : Controller 
{ 
    private ApplicationSignInManager _signInManager; 
    private ApplicationUserManager _userManager; 

    public AccountController() 
    { 
    } 

    public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager) 
    { 
     UserManager = userManager; 
     SignInManager = signInManager; 
    } 

    public ApplicationSignInManager SignInManager 
    { 
     get 
     { 
      return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>(); 
     } 
     private set 
     { 
      _signInManager = value; 
     } 
    } 

    public ApplicationUserManager UserManager 
    { 
     get 
     { 
      return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
     } 
     private set 
     { 
      _userManager = value; 
     } 
    } 

    // 
    // GET: /Account/Login 
    [AllowAnonymous] 
    public ActionResult Login(string returnUrl) 
    { 
     ViewBag.ReturnUrl = returnUrl; 
     return View(); 
    } 
} 

更新2:

正如史蒂芬建議,我已經從工廠取出的try-catch,並得到此異常:

An exception of type 'Microsoft.Practices.Unity.ResolutionFailedException' occurred in Microsoft.Practices.Unity.dll but was not handled in user code 

Additional information: Resolution of the dependency failed, type = "WebApplication1.Controllers.AccountController", name = "(none)". 

Exception occurred while: while resolving. 

Exception is: InvalidOperationException - The current type, Microsoft.AspNet.Identity.IUserStore`1[WebApplication1.Models.ApplicationUser], is an interface and cannot be constructed. Are you missing a type mapping? 

----------------------------------------------- 

At the time of the exception, the container was: 



    Resolving WebApplication1.Controllers.AccountController,(none) 

    Resolving parameter "userManager" of constructor WebApplication1.Controllers.AccountController(WebApplication1.ApplicationUserManager userManager, WebApplication1.ApplicationSignInManager signInManager) 

    Resolving WebApplication1.ApplicationUserManager,(none) 

    Resolving parameter "store" of constructor WebApplication1.ApplicationUserManager(Microsoft.AspNet.Identity.IUserStore`1[[WebApplication1.Models.ApplicationUser, WebApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] store) 

     Resolving Microsoft.AspNet.Identity.IUserStore`1[WebApplication1.Models.ApplicationUser],(none) 
+0

顯示'AccountController'構造函數或全班 – Backs

+0

我覺得你得到的異常信息是很清楚:「目前的類型,'IUserStore ',是一個接口,不能構成是否缺少。類型映射?「。你錯過了一個映射。 – Steven

回答

1

我已修正該錯誤:可以剝離該方法下降到。

container.RegisterType<AccountController>(new InjectionConstructor()); 
container.RegisterType<ManageController>(new InjectionConstructor()); 
0

刪除try - catch st從您的自定義ControllerFactory.GetControllerInstance中得到答案,您將立即看到問題所在。通過添加以下兩行中的引導程序的BuildUnityContainer()方法

protected override IController GetControllerInstance(RequestContext requestContext, 
    Type controllerType) 
{ 
    return (IController)MvcUnityContainer.Container.Resolve(controllerType); 
} 
+0

好的建議。我對結果進行了更新。 – Khaine775