I'm currently struggling to get inheritance in WebApi working like I'd like to. Maybe you can point me in the right direction?
My goal is to have a generic WebApi project, which can be reused in multiple other WebApi projects.
For example: I have a reusable UserController which can handle the login for multiple WebSites. Now I build a new WebSite and simple add the UserController to the newly created project, which simply works.
But now the requirement for the new WebSite changes and I need to overload the UserController methods to reflect these changes without modifying the generic controller.
What I want to achieve can be seen in the following code snippet (of course this won't compile because C# complains not suitable method to overload found.)
// This belongs to solution "GenericWebApi"
public class LoginRequest
{
public string User { get; set; }
}
public class BaseApiController : ApiController
{
[Route("login")]
public virtual HttpResponseMessage Login(LoginRequest request)
{
return new HttpResponseMessage();
}
}
// This belongs to solution "CarWebsite"
public class CarWebsiteLoginRequest : LoginRequest
{
public string Car { get; set; }
}
[RoutePrefix("api/users")]
public class CarWebsiteApiController : BaseApiController
{
public override HttpResponseMessage Login(CarWebsiteLoginRequest request)
{
// Do some car specific login stuff
return base.Login(request);
}
}
One solution I found to be working was replacing the "LoginRequest" and "CarWebsiteLoginRequest" with "dynamic", but that feels simply wrong and the ModelBinder of course will not work as intended.
Any ideas how you would solve that?
Thanks in advance