0
When performing a post for my controller, when debugging, I saw that the service I load in my constructor, is coming type null.
Controller:
public class UserController : ApiController
{
private static IUserService _userService;
public UserController(IUserService userService)
{
_userService = userService;
}
[System.Web.Http.HttpGet]
public IHttpActionResult GetUserById(long id)
{
var user = _userService.GetById(id);
return Ok(user);
}
[HttpPost]
[Route("api/User/Insert")]
public IHttpActionResult Insert(User user)
{
user.StartDate = DateTime.Now;
_userService.Register(user);
return Ok();
}
}
Return of debbug error:
Controller' does not have a default constructor","Exception Type":"System.Argumentexception","Stacktrace"
You initialize the
UserController
at some point?– rLinhares
What dependency injection strategy do you use?
– Diego Rafael Souza
@Diegorafaelsouza was trying to inject into Webapiconfig.Cs, but it was unsuccessful. So, it’s none.
– user103979
So there’s no way it’s gonna work unless you declare such a default constructor, like,:
public UserController() : this(new UserServiceImplementation()) { }
– Diego Rafael Souza
That way it doesn’t work.
– user103979