MVC 4:未找到控制器说WindsorControllerFactory

时间:2013-06-13 08:03:28

标签: asp.net-mvc asp.net-mvc-4 castle-windsor

我有这个继承自WindsorControllerFactory

的班级DefaultControllerFactory
public class WindsorControllerFactory : DefaultControllerFactory
    {
        private readonly IKernel _container;

        public WindsorControllerFactory(IKernel container)
        {
            _container = container;
        }

        protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, System.Type controllerType)
        {
            if(controllerType == null)
            {
                throw new HttpException(404, "Controller not found.");
            }
            return _container.Resolve(controllerType) as IController;//exception here no controller found
        }
    }

在我的帐户控制器中我有登录功能  [授权]

    public class AccountController : Controller
    {
        //
        // GET: /Account/Login

        [AllowAnonymous]
        public ActionResult Login()
        {

            return View();

        }

        //
        // POST: /Account/Login

        [HttpPost]
        [AllowAnonymous]
//        [ValidateAntiForgeryToken]
        public ActionResult Login(LoginModel model)
        {
            if(Membership.ValidateUser(model.UserName, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);

                RedirectToAction("Index", "Home");
            }

            // If we got this far, something failed, redisplay form
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return View(model);
        }
    }

当我运行应用程序时,http://localhost:58383/Account/Login我在WindsorControllerFactory类中收到错误,说找不到控制器。 知道为什么会这样吗?

2 个答案:

答案 0 :(得分:1)

您需要告诉mvc框架使用您的windsor工厂而不是默认工厂。

根据Siva提到的windsor文档示例,我强烈建议您重写ReleaseController ......

您还需要将控制器注册到容器中。

我假设您正确设置了路由...这与windsor无关。

答案 1 :(得分:0)

您应首先将组件注册到Windsor Container。