0

I am getting the following error while running my MVC application, which uses the json data:

An error occurred while creating a controller of type 'ecom.Controllers.AdminController'. If the controller doesn't have a controller factory, ensure that it has a parameterless public constructor."

How can I fix this?

2
  • 1
    As error says may be you don't have parameterless public constructor? Commented Oct 27, 2009 at 12:02
  • Or check whether there's code in your constructor throwing an exception: stackoverflow.com/questions/438870/… Commented Oct 27, 2009 at 12:06

3 Answers 3

2

If you've provided a non-default constructor for your controller, one which takes parameters, (say, for testing) you need to also provide suitable default, parameterless constructor that doesn't take parameters so that the default controller factory can instantiate your controller. If your controller must have parameters supplied to it and can't use a default, parameterless constructor, then you must implement a controller factory for it that knows how to instantiate it with the parameters.

Sign up to request clarification or add additional context in comments.

2 Comments

+1. Absolutely. I would add if you need to inject something into the controllers, then using something like Unity would be useful.
Or Castle.Windsor, Ninject, StructureMap, Autofac, etc. Just throwing out options.
0

By default, the class you derive from "Controller" (e.g. "MyController") doesn't have a constructor. If you add one that adds parameters (for whatever reason) then you'll also need a parameterless version. It has nothing to do with json as far as I know.

1 Comment

This is true unless you are using IOC.
0

Try giving it a constructor - public , no parameters

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.