I am making a custom controller class, from which all my other controller classes will inherit in .Net Framework 4.8, Web API
public class BaseApiController<T> where T: class, ApiController
{
protected T service;
public BaseApiController(T service)
{
this.service = service;
}
}
This gives a compile-time error
Error CS0450 'ApiController': cannot specify both a constraint class and the 'class' or 'struct' constraint
I know I am missing small detail with the right syntax.
T is a service, which is individual for every controller and I want to make sure it is always a reference type. I also want to inherit from the main ApiController class.
If I do it like this, there are no errors (This works for me):
public class BaseApiController<T> : ApiController
{
protected T service;
public BaseApiController(T service)
{
this.service = service;
}
}
But I want the additional T logic for reference type.
I want to use it like this, e.g:
Note: This how I use it and it works.
[Authorize]
[RoutePrefix("api/[controller]")]
public class MyOtherController : BaseApiController<IMyOtherService>
{
public MyOtherController(IMyOtherService myOtherService) : base(myOtherService)
{
}
[HttpPost]
public IHttpActionResult DoSomething(Model requestModel)
{
var result = this.service.SetPaymentStatusUK(model: requestModel);
return Ok();
}
}
I just want to know how to add the T check for reference type in the BaseApiController class.
structcould inherit fromApiController, what do you thinkclassis adding for you here?class SpecialNodeItem<T> : NodeItem<T> where T : System.IComparable<T>, new() { }learn.microsoft.com/en-us/dotnet/csharp/programming-guide/…