• Register
1 vote
67 views

Problem :

Recently I was told to prepare the C# as I am in pool of new projects. So I have started learning C#. But because of some unknown reason to me now I am facing below error.

Make sure that the controller has a parameterless public constructor”.

As I am very new to C# so I am unable to understand the above error.

I need help from someone who can fix the above error.

8 5 2
3,230 points

1 Answer

1 vote

Solution :

I can tell you what must have happened with you is that you must have forgotten to register your controllers explicitly in the container. Now the Unity will try to fix the unregistered concrete types for you but as it is unable to resolve it so it will return null. Now the Web API will try to create your controller itself but as it does  not have the default constructor in this case it will throw the above error listed by you.

This error message is certainly misleading for beginners like you and it fail to explain you the real cause.

This is the configuration error and it comes because of adding the second constructor to the DbContext. Now the Unity will try to pick a constructor which is having the most arguments, but here it has no idea how to fix this particular constructor.

It seems you are trying to use a Unity's auto wiring capabilities simply to create a DbContext. I must tell you the DbContext is the most special type and so it shouldn't be auto wired. As it is the framework type so you should fallback to the registering it using the factory delegate.

 

8 4
5,680 points

Related questions

1 vote
1 answer 34 views
34 views
Problem : While binding the dropdown in MVC, I often face following error: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key country. Please find below my View for your reference : @Html.DropDownList("country", (IEnumerable<SelectListItem>)ViewBag ... .countryid.ToString() }); } ViewBag.mycountrydrop = myitem8; return View(); I do not know how to resolve above error.
asked Dec 28, 2019 alecxe 7.5k points
0 votes
1 answer 70 views
70 views
Problem : I have recently installed a website on my local machine using IIS 7 successfully. But when I tried to deploy it on the live server, I got a following error: "The page cannot be displayed because an internal server error has occurred" Nothing else. Using a same IIS 7 on live and also set to have the Detailed errors in Error Pages module, but still getting a same. What can be the reason?
asked Jan 28 jwilliam 3.9k points
0 votes
1 answer 8 views
8 views
Problem: I am trying to enable CORS on one of the very specific actions in my Asp.net Web Api. Please find below my code and tell me if I am missing anything: [Route("api/mycontroller/myaction")] [HttpPost] [EnableCors("https://example.com", "*", "post")] public ... http method 'OPTIONS'." I also tried to remove the [HttpPost] annotation but to no avail. Have I made any mistake in my above code?
asked Aug 19 Raphael Pacheco 4.9k points
0 votes
1 answer 5 views
0 votes
1 answer 8 views
8 views
Problem: I am naive to Asp.Net.I am having a weird looking problem where I am trying to open up my ASP.NET MVC application but I am receiving the below ASP.NET error page which says: Server Error in '/' Application. The resource cannot be found. Description: ... unknown reason to me it seems like it is actually searching for the .aspx file. Please guide me if you think I am missing something.
asked Aug 19 Raphael Pacheco 4.9k points