• Register
1 vote
352 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

Please log in or register to answer this question.

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 1
5,680 points

Related questions

0 votes
1 answer 16 views
16 views
Problem: the requested resource does not support http method 'get'.
asked Feb 20 charles mathews 5.5k points
1 vote
1 answer 92 views
92 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 42 views
42 views
Problem: Below is WebAPI action. On googling about the below error: The requested resource does not support HTTP method 'POST' Any help/suggestion highly appreciated. Thanks.
asked Mar 20 ummesalma 29.2k points
0 votes
1 answer 29 views
29 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, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 7 views
7 views
Problem: I have a web application that uses Forms Authentication. The application is MVC and also has a section for requests from our Mobile application. The mobile application for a client was unused for a while and came back and now I see this logged in ... at this post: ASP.NET Forms Authentication failed for the request. Reason: The ticket supplied has expired Any help is appreciated. Thanks,
asked Apr 25 Humaira ahmed 50.7k points
0 votes
1 answer 158 views
158 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, 2020 jwilliam 3.9k points
0 votes
1 answer 22 views
22 views
Problem: This request has been blocked; the content must be served over https.
asked Feb 15 Ethan ross 2.7k points
0 votes
1 answer 4 views
4 views
Problem: I have some code and when it executes, it throws an exception saying: The model item passed into the dictionary is of type Bar but this dictionary requires a model item of type Foo What does this mean, and how do I fix it?
asked Mar 20 ummesalma 29.2k points
1 vote
1 answer 27 views
27 views
Problem : Why I got the error : The compiler failed with error code -2146232576 Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34209
asked Mar 10 Sabbir Hossain 14.6k points
0 votes
1 answer 13 views