Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp.net中带变量的类级路由_C#_Asp.net - Fatal编程技术网

C# asp.net中带变量的类级路由

C# asp.net中带变量的类级路由,c#,asp.net,C#,Asp.net,是否可以有一个以变量结尾的类级别路由? 例: 如果是这样,您将如何访问该变量 还是我只是完全错了?我有两个对象,它们都来自同一个抽象类,现在每个对象都有一个不同的控制器,但实际上它们都是相同的。我认为可以使用泛型方法来处理所有问题,但不确定解释对象实际类型的最佳方法 例: [路由(“api/{carType}”)] 公共类控制器:控制器 { [HttpGet()] 公共IActionResult GetCars() { 结果; 开关(carType){ 案例“福特”: 结果=myService.

是否可以有一个以变量结尾的类级别
路由
? 例:

如果是这样,您将如何访问该变量

还是我只是完全错了?我有两个对象,它们都来自同一个抽象类,现在每个对象都有一个不同的控制器,但实际上它们都是相同的。我认为可以使用泛型方法来处理所有问题,但不确定解释对象实际类型的最佳方法

例:

[路由(“api/{carType}”)]
公共类控制器:控制器
{
[HttpGet()]
公共IActionResult GetCars()
{
结果;
开关(carType){
案例“福特”:
结果=myService.GetCars();
打破
案例“丰田”:
结果=myService.GetCars();
打破
....
}
返回结果;
}
}

是的,我已经做过很多次了。您将创建一个路由,该路由映射到一个采用类名的泛型操作,然后使用反射对该操作调用您想要的任何方法

是的,我已经做过很多次了。您将创建一个路由,该路由映射到一个采用类名的泛型操作,然后使用反射对该操作调用您想要的任何方法

是的,您可以按照以下示例进行操作:

[RoutePrefix("customers/{customerId}")]
public class OrdersController : ApiController
{
    // GET customers/1/orders
    [Route("orders")]
    public IEnumerable<Order> Get(int customerId) 
    { 
      ... 
    }
}
[RoutePrefix(“customers/{customerId}”)]
公共类OrdersController:ApicController
{
//获取客户/1/订单
[路线(“订单”)]
公共IEnumerable Get(int customerId)
{ 
... 
}
}

是的,您始终可以按照以下示例进行操作:

[RoutePrefix("customers/{customerId}")]
public class OrdersController : ApiController
{
    // GET customers/1/orders
    [Route("orders")]
    public IEnumerable<Order> Get(int customerId) 
    { 
      ... 
    }
}
[RoutePrefix(“customers/{customerId}”)]
公共类OrdersController:ApicController
{
//获取客户/1/订单
[路线(“订单”)]
公共IEnumerable Get(int customerId)
{ 
... 
}
}

我明白了,您不能将carType添加为GetCars的参数吗?比如“public IActionResult GetCars(string carType)”?但看起来并没有那么干净。也许会重新思考我是如何处理它的,我明白了,难道你不能仅仅添加carType作为GetCars的参数吗?比如“public IActionResult GetCars(string carType)”?但看起来并没有那么干净。可能会重新考虑我该怎么做
[RoutePrefix("customers/{customerId}")]
public class OrdersController : ApiController
{
    // GET customers/1/orders
    [Route("orders")]
    public IEnumerable<Order> Get(int customerId) 
    { 
      ... 
    }
}