简体   繁体   English

ASP Net Core MVC创建实例控制器System.MissingMethodException:'为此对象未定义无参数构造函数。

[英]asp net core mvc create instance controller System.MissingMethodException: 'No parameterless constructor defined for this object.'

How can I fix this issue System.MissingMethodException: 'No parameterless constructor defined for this object.' 如何解决此问题System.MissingMethodException: 'No parameterless constructor defined for this object.' I followed this code from codeproject CodeProject Code . 我遵循了codeproject CodeProject Code的代码

Already tried adding blank constructor in Controller but it's giving me another error. 已经尝试在Controller中添加空白构造函数,但这给了我另一个错误。 InvalidOperationException: Multiple constructors accepting all given argument types have been found in type 'SlhSd18.Controllers.FunctionsController'. There should only be one applicable constructor.

Here is my model code: 这是我的模型代码:

    namespace SlhSd18.Models
{
    public class Function
    {
        public int Id { get; set; }

        [Required, StringLength(10)]
        [CustomRemoteValidation("IsFunctionIdExist", "Functions", AdditionalFields = "Id",
            ErrorMessage = "Function Id already exist.")]
        public string FunctionId { get; set; }

Controller Code: 控制器代码:

    namespace SlhSd18.Controllers
{
    public class FunctionsController : Controller
    {
        private readonly SlhSd18Context _context;

        public FunctionsController(SlhSd18Context context)
        {
            _context = context;
        }

        public IActionResult IsFunctionIdExist(string functionId, int ? Id)
        {
            bool validate = _context.Function.Any
                (x => x.FunctionId == functionId && x.Id != Id);

            if (validate ==true)
            {
                return Json(false);
            }
            else
            {
                return Json(true);
            }
        }

CustomRemoteValidation Code: CustomRemoteValidation代码: 在此处输入图片说明

SlhSd18 Class Code SlhSd18类代码

    namespace SlhSd18.Models
{
    public class SlhSd18Context : DbContext
    {
        public SlhSd18Context (DbContextOptions<SlhSd18Context> options)
            : base(options)
        {
        }

        public DbSet<SlhSd18.Models.Movie> Movie { get; set; }

        public DbSet<SlhSd18.Models.ChartofAccount> ChartofAccount { get; set; }

        public DbSet<SlhSd18.Models.Function> Function { get; set; }

    }
}

I don't know setup of your DI, but I suggest to use this method to create controller instance: 我不知道您的DI的设置,但我建议使用此方法来创建控制器实例:

private object TryCreateController(ValidationContext context, string controllerName) {
    Type controllerType = Assembly.GetExecutingAssembly().GetTypes().Single(x => x.Name.ToString().ToLower() == controllerName+"Controller");
    if (controllerType == null) {
        return null;
    }
    foreach (var constructor in controllerType.GetConstructors()) {
        var parameters = constructor.GetParameters();
        var args = new dynamic[parameters.Length];
        for (int i = 0; i < parameters.Length; i++) {
            args[i] = context.GetService(parameters[i].ParameterType);
        }

        try {
            var instance = Activator.CreateInstance(controllerType, args);
            if (instance != null) {
                return instance;
            }
        }
        catch {
            continue;
        }

    }

    return null;
}

And you can change the highlighted line to the next: 您可以将突出显示的行更改为下一行:

object instance = TryCreateController(validationContext, this.RouteData["controller"].ToString().ToLower());

暂无
暂无

声明:本站的技术帖子网页,遵循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 System.MissingMethodException:没有为此对象定义无参数构造函数 - System.MissingMethodException: No parameterless constructor defined for this object EF Core Add-Migration 因 System.MissingMethodException 失败:未定义无参数构造函数 - EF Core Add-Migration fails with System.MissingMethodException: No parameterless constructor defined 未处理的异常。 System.MissingMethodException:没有为类型定义无参数构造函数 - Unhandled exception. System.MissingMethodException: No parameterless constructor defined for type 没有为此对象定义无参数构造函数。 MVC 5 - No parameterless constructor defined for this object. MVC 5 错误 System.MissingMethodException:没有为“System.Linq.Expressions.Expression”类型定义无参数构造函数 - Error System.MissingMethodException: No parameterless constructor defined for type of 'System.Linq.Expressions.Expression`1 MissingMethodException:没有为此对象定义无参数构造函数 - MissingMethodException: no parameterless constructor defined for this object 温莎城堡+ ASP MVC 5-System.MissingMethodException:无法创建接口的实例 - Castle Windsor + ASP MVC 5 - System.MissingMethodException: Cannot create an instance of an interface ASP.NET MVC 4 + Ninject MVC 3 = 没有为此对象定义无参数构造函数 - ASP.NET MVC 4 + Ninject MVC 3 = No parameterless constructor defined for this object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM