0

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.

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:

                 public interface ICompanyService
                {
                 string GetCompany();
                }

CompanyService:

                   public class CompanyService
                   {
                      public string GetCompany()
                     {
                         return "Msc";
                     }
                 }
Shashi
  • 29
  • 7

3 Answers3

1

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!!!!!

BV Winoya
  • 199
  • 2
  • 12
0

CompanyService Class Should Inherits From ICompanyService Interface. Please Study About Dependency Injection In .NET .

 public class CompanyService : ICompanyService
                   {
                      public string GetCompany()
                     {
                         return "Msc";
                     }
                   }
Amin Golmahalleh
  • 3,585
  • 2
  • 23
  • 36
  • still it is throwing same issue – Shashi May 07 '19 at 10:43
  • Beacuse You Don't Use Dependency Injection In Project. Configure Like The 'Auto Fac' Or 'Ninject' Or 'Unity' In Project. Constructor Controller By Defualt Don't Have Parameter. – Amin Golmahalleh May 07 '19 at 10:55
  • I hope this link will help: https://stackoverflow.com/questions/122273/constructor-parameters-for-controllers-without-a-di-container-for-asp-net-mvc – Amin Golmahalleh May 07 '19 at 11:02
0

@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. don't worry about initialization.

below i have mention the link for IoC and i hope your issue will resolve soon. https://github.com/quozd/awesome-dotnet/blob/master/README.md#ioc

Shahzad Khan
  • 432
  • 2
  • 14