我想在Global.asax.cs中以编程方式注册控制器.使用MvcContrib.Castle.WindsorControllerFactory
private static IWindsorContainer _Container;
protected virtual void InitializeWindsor()
{
try
{
if (_Container == null)
{
_Container = new WindsorContainer();
ControllerBuilder.Current.SetControllerFactory(new MvcContrib.Castle.WindsorControllerFactory(_Container));
RegisterActiveRecord();
RegisterRepositories();
RegisterServices();
RegisterControllers();
RegisterComponents();
}
Run Code Online (Sandbox Code Playgroud)
我已经做到了
private void RegisterControllers()
{
try
{
_Container.Register(
AllTypes.Of<IController>()
.FromAssembly(typeof(HomeController).Assembly)
.Configure(c => c.LifeStyle.Transient)
);
Run Code Online (Sandbox Code Playgroud)
它适用于具有默认构造函数的所有控制器.但是,我有一个带参数化construtor的控制器(LoginController).
public class LoginController : Controller
{
private IUser _User;
public LoginController(IUser objUser)
{
_User = objUser;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试在浏览器中查看它时(http:// localhost:2011/Login),它会给我以下错误.
**No parameterless constructor defined for this object**
Stack Trace:
[MissingMethodException: No parameterless constructor defined for …Run Code Online (Sandbox Code Playgroud) asp.net-mvc configuration controller castle-windsor global-asax