Is it possible to have a class level Route
that ends in a variable? Ex:
[Route("api/{carType}")]
public class CarController : Controller
{
..rest of class
}
and if so how would you access that variable?
Or am I just thinking about this completely wrong? I have a couple of objects that all derive from the same abstract class and right now I have a different controller for each but they are all the same practically. I think it would be possible to have generic methods to handle everything but not sure the best way to interpret what type the object actually is.
Ex:
[Route("api/{carType}")]
public class CarController : Controller
{
[HttpGet()]
public IActionResult GetCars()
{
IActionResult result;
switch(carType){
case "Ford":
result = myService.GetCars<Ford>();
break;
case "Toyota":
result = myService.GetCars<Toyota>();
break;
....
}
return result;
}
}
Yes, I have done this many times. You would create a route which maps to a generic action that takes the class name and then using reflection, invokes whatever method you want on that action.
Yes, you can always do it as in following example:
[RoutePrefix("customers/{customerId}")]
public class OrdersController : ApiController
{
// GET customers/1/orders
[Route("orders")]
public IEnumerable<Order> Get(int customerId)
{
...
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.