简体   繁体   English

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

[英]MVC:No parameterless constructor defined for this object

Server Error in '/' Application. '/'应用程序中的服务器错误。 No parameterless constructor defined for this object.How can I resolve this issue. 没有为此对象定义无参数构造函数。如何解决此问题。 I created one folder in that created interface ICompanyService and class CompanyService. 我在创建的接口ICompanyService和类CompanyService中创建了一个文件夹。

Controller: 控制器:

         public class HomeController : Controller
         {
               private ICompanyService icompanyService;
               public HomeController(ICompanyService icompanyService)
              {
                 this.icompanyService = icompanyService;
               }
                public ActionResult Index()
              {     
                 ViewBag.CompanyName = this.icompanyService.GetCompany();
                return View();
              }
         }

ICompanyService: ICompanyService:

                 public interface ICompanyService
                {
                 string GetCompany();
                }

CompanyService: CompanyService:

                   public class CompanyService
                   {
                      public string GetCompany()
                     {
                         return "Msc";
                     }
                 }

CompanyService Class Should Inherits From ICompanyService Interface. CompanyService类应该从ICompanyService接口继承。 Please Study About Dependency Injection In .NET . 请研究.NET中的依赖注入。

 public class CompanyService : ICompanyService
                   {
                      public string GetCompany()
                     {
                         return "Msc";
                     }
                   }

You need to include below constructor to your controller, 您需要在控制器中包含以下构造函数,

        public HomeController() : this(new CompanyService())
            {
            }

So your entire controller code looks like below, 所以你的整个控制器代码如下所示,

    public class HomeController : Controller
{
    private ICompanyService icompanyService;

    public HomeController() : this(new CompanyService())
    {
    }
    public HomeController(ICompanyService icompanyService)
    {
        this.icompanyService = icompanyService;
    }
    public ActionResult Index()
    {
        ViewBag.CompanyName = this.icompanyService.GetCompany();

        return View();
    }
}

This will solve your issue. 这将解决您的问题。

Happy coding!!!!! 快乐的编码!!!!!

@ravi please use dependency injection, so what service will automatically initialize your service constructor without defined constructor logic, the dependency inject handle and initialize you service object. @ravi请使用依赖注入,那么什么服务会在没有定义构造函数逻辑的情况下自动初始化服务构造函数,依赖注入句柄并初始化服务对象。 don't worry about initialization. 不要担心初始化。

below i have mention the link for IoC and i hope your issue will resolve soon. 下面我提到了IoC的链接,我希望你的问题很快就能解决。 https://github.com/quozd/awesome-dotnet/blob/master/README.md#ioc https://github.com/quozd/awesome-dotnet/blob/master/README.md#ioc

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM