简体   繁体   English

没有为此对象MVC4定义无参数的构造函数

[英]No parameterless constructor defined for this object MVC4

This question already asked so many time and also tried and created default contructor for my model class but still im getting error.below are my code lines, i want to know if somebody can help me to fix my code.. 这个问题已经问了很多次了,还尝试为我的模型类创建默认构造函数,但是仍然出现错误。下面是我的代码行,我想知道是否有人可以帮助我修复我的代码。

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

    }

Below is my TestRecordModel class 下面是我的TestRecordModel类

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

        }

Below is my bootstrapper,WindsorControllerActivator and ControllerInstaller 以下是我的引导程序,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),则将收到有关无参数构造函数的错误。

The error you are getting is the standard error when the mvc framework creates a controller. 当mvc框架创建控制器时,您得到的错误是标准错误。 I Think that you registration/bootstrapper is not being called correctly. 我认为您的注册/引导程序未正确调用。 Set a breakpoint inside WindsorControllerActivator to see if it gets called. 在WindsorControllerActivator中设置一个断点以查看是否被调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.MissingMethodException:没有为此对象定义的无参数构造函数。 MVC4 - System.MissingMethodException: No parameterless constructor defined for this object. MVC4 System.MissingMethodException:在mvc4中没有为此对象定义无参数构造函数 - System.MissingMethodException: No parameterless constructor defined for this object in mvc4 在mvc中没有为此对象定义无参数构造函数 - No parameterless constructor defined for this object in mvc 混合Unity Web API和MVC4 UnityDependencyResolvers:没有为此对象定义无参数构造函数 - Mixing Unity Web API and MVC4 UnityDependencyResolvers: No parameterless constructor defined for this object 在MVC中没有为此对象错误定义无参数构造函数 - No parameterless constructor defined for this object error in MVC 没有为此对象定义无参数构造函数-MVC项目 - No parameterless constructor defined for this object - MVC project 没有为此对象定义无参数构造函数。 MVC 5 - No parameterless constructor defined for this object. MVC 5 没有为此对象定义无参数构造函数 - No parameterless constructor defined for this object ASP.NET MVC 4 + Ninject MVC 3 = 没有为此对象定义无参数构造函数 - ASP.NET MVC 4 + Ninject MVC 3 = No parameterless constructor defined for this object Ajax调用MVC操作方法-没有为此对象定义无参数构造函数 - Ajax calling MVC action method - No parameterless constructor defined for this object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM