Cybercop
Cybercop

Reputation: 8674

MVC 4 : Controller not found says WindsorControllerFactory

I have this class WindsorControllerFactory which inherits from 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
        }
    }

in my account controller I have Login function [Authorize]

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

when I run the application, http://localhost:58383/Account/Login I get error in that WindsorControllerFactory class, says controller not found. Any idea why is that happening?

Upvotes: 1

Views: 1255

Answers (2)

Jason Li
Jason Li

Reputation: 1585

You should register components first to Windsor Container.

Upvotes: 0

Crixo
Crixo

Reputation: 3070

You need to tell to the mvc framework to use your windsor factory instead of the default one.

As per windsor documentation sample mentioned by Siva, I strongly suggest you to override ReleaseController as well...

You also need to register the controllers into the container.

I assume you set up properly the routing... that's nothing to do with windsor.

Upvotes: 1

Related Questions