簡體   English   中英

沒有為此對象MVC4定義無參數的構造函數

[英]No parameterless constructor defined for this object MVC4

這個問題已經問了很多次了,還嘗試為我的模型類創建默認構造函數,但是仍然出現錯誤。下面是我的代碼行,我想知道是否有人可以幫助我修復我的代碼。

public class RecordController: Controller
    {
        private readonly IRecord  _DataService = null;

        public RecordController(IRecord dataService )
        {
            _DataService = dataService ;
        }

        [HttpGet]
        public ActionResult TestRecord()
        {
            return View();
        }

        [HttpPost]
        public ActionResult TestRecord(TestRecordModel model)
        {         

            return View();
        }

    }

下面是我的TestRecordModel類

public class TestRecordModel 
        {
            [Required]
            [Display(Name = "UserNo #:)]
            public string UserNo { get; set; }

        }

以下是我的引導程序,WindsorControllerActivator和ControllerInstaller

 public class Bootstrapper
    {
        #region Properties

        public static IWindsorContainer Container { get; private set; }

        #endregion

        /// <summary>
        /// Initialises this instance.
        /// </summary>
        public static void RegisterAllTypes()
        {
            // adds and configures all components using WindsorInstallers from executing assembly.
            Container = new WindsorContainer().Install(FromAssembly.InThisApplication());

            Container.Register(Component.For<IViewEngine>().ImplementedBy<RazorViewEngine>());
            Container.Register(Component.For<IControllerFactory>().ImplementedBy<WindsorControllerFactory>());
            Container.Register(Component.For<IControllerActivator>().ImplementedBy<WindsorControllerActivator>());
            Container.Register(Component.For<IHttpControllerActivator>().ImplementedBy<WindsorHttpControllerActivator>());

            DependencyResolver.SetResolver(new WindsorDependencyResolver(Container.Kernel));

            GlobalConfiguration.Configuration.DependencyResolver = new WindsorHttpDependencyResolver(Container.Kernel);
        }

        public static void RegisterType<TContract, TImplementation>(params KeyValuePair<string, string>[] parameters)
            where TContract : class
            where TImplementation : TContract
        {
            var dependencies = parameters
                .Select(parameter => Parameter.ForKey(parameter.Key).Eq(parameter.Value))
                .Select(dummy => (Dependency)dummy).ToArray();

            Container.Register(Component.For<TContract>()
                                   .ImplementedBy<TImplementation>()
                                   .DependsOn(dependencies));
        }

        public static TContract Resolve<TContract>()
        {
            return Container.Resolve<TContract>();
        } 

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        public static void Dispose()
        {
            // clean up, application exits.
            if (Container != null)
                Container.Dispose();
        }
    }





 public class WindsorControllerActivator : IControllerActivator
    {
        #region Private Members

        private readonly IKernel _kernel;

        #endregion

        #region Constructor(s)

        /// <summary>
        /// Initializes a new instance of the <see cref="WindsorControllerActivator" /> class.
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        public WindsorControllerActivator(IKernel kernel)
        {
            _kernel = kernel;
        }

        #endregion

        #region IControllerActivator Members

        /// <summary>
        /// When implemented in a class, creates a controller.
        /// </summary>
        /// <param name="requestContext">The request context.</param>
        /// <param name="controllerType">The controller type.</param>
        /// <returns>
        /// The created controller.
        /// </returns>
        public IController Create(RequestContext requestContext, Type controllerType)
        {
            return (IController)_kernel.Resolve(controllerType);
        }

        #endregion
    }







public class ControllerInstaller : InstallerBase
    {
        /// <summary>
        /// Performs the installation in the <see cref="T:Castle.Windsor.IWindsorContainer" />.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="store">The configuration store.</param>
        /// <exception cref="System.NotImplementedException"></exception>
        public override void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(
                Classes.FromAssemblyInDirectory(new AssemblyFilter(GetExecutingDirectory()))
                    .BasedOn<IController>()
                    .LifestyleTransient());
        } 
    }

也許如果您嘗試使用serialize或Activator.CreateInstance(t),則將收到有關無參數構造函數的錯誤。

當mvc框架創建控制器時,您得到的錯誤是標准錯誤。 我認為您的注冊/引導程序未正確調用。 在WindsorControllerActivator中設置一個斷點以查看是否被調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM