ASP MVC ControllerFactory在AccountController上失败

问题描述 投票:1回答:3

我正在使用Unity,即使我不应该注册我的控制器(据我所知),我在我的AccountController上得到以下错误,这只是从默认的MVC模板中取出的一个新项目。

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...

我不知道从哪里开始,任何帮助表示赞赏。

更新:

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)
c# asp.net-mvc dependency-injection unity-container
3个回答
1
投票

我通过将以下两行添加到Bootstrapper中的BuildUnityContainer()方法来修复错误。

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

1
投票

我在安装Unity.MVC后遇到了这个问题。我在登录/退出网站时遇到错误。结果是控制器构造函数必须在Unity参数和身份参数之间分开,如下所示:

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

public AccountController(IUserRepository _repUser, IConfigRepository _repConfig)
{
    repUser = _repUser;
    repConfig = _repConfig;
}

0
投票

从您的自定义try中删除catch-ControllerFactory.GetControllerInstance语句,您将立即看到问题所在。您可以将该方法剥离为:

protected override IController GetControllerInstance(RequestContext requestContext, 
    Type controllerType)
{
    return (IController)MvcUnityContainer.Container.Resolve(controllerType);
}
© www.soinside.com 2019 - 2024. All rights reserved.