C# 如何为webapi配置此路由?
我有一个控制器来获取一个资源(比如说Employee),它有两个属性(比如CategoryId和DepartmentId)。我需要配置路由以支持以下URL:C# 如何为webapi配置此路由?,c#,asp.net-web-api,routes,C#,Asp.net Web Api,Routes,我有一个控制器来获取一个资源(比如说Employee),它有两个属性(比如CategoryId和DepartmentId)。我需要配置路由以支持以下URL: ~/api/employees/1234 [to get the employee with employeeId=1234] ~/api/employees [to get all the employees] ~/api/employees?departmentid=1 [to get all the employees with de
~/api/employees/1234 [to get the employee with employeeId=1234]
~/api/employees [to get all the employees]
~/api/employees?departmentid=1 [to get all the employees with departmentId=1]
控制器代码如下所示:
public IEnumerable<Employee> Get()
{
....
}
public IEnumerable<Employee> Get(int employeeId, int departmentId = -1, int categoryId = -1)
{
.....
}
public IEnumerable Get()
{
....
}
public IEnumerable Get(int-employeeId,int-departmentId=-1,int-categoryId=-1)
{
.....
}
如何配置此控制器的路由
感谢所有QueryTyring参数,在路由方面没有什么可做的:只需让controller方法parmater与qs参数名称匹配(不区分大小写) 如果您的方法参数引用uri段,则方法参数名称必须与花括号之间的路由参数/段匹配
routes.MapHttpRoute(
name: "API Default",
routeTemplate: "/api/{controller}/{employeeId}",
defaults: new { id = RouteParameter.Optional }
);
这意味着您需要一个具有以下方法的控制器
public class EmployeesController : ApiController
{
public IEnumerable<Employee> Get(int employeeId){...}
}
公共类EmployeesController:ApicController
{
公共IEnumerable Get(int employeeId){…}
}
请记住,除非使用action,否则在控制器上,每个http谓词只能使用一个方法。换句话说,除非您对这两种方法都使用显式操作,否则示例中的动词get有2个方法是行不通的。您看过使用属性路由吗?我们现在广泛使用属性路由,已经完全摆脱了MapHttpRoute的默认/控制器/操作类型路由 相反,我们装饰我们的控制器如下。首先,我们为控制器创建一个路由前缀,以便我们知道我们需要的基本路由是什么
/// <summary> A controller for handling products. </summary>
[RoutePrefix("api/purchasing/locations/{locationid}/products")]
public class ProductsController : PurchasingController
{
因此,对的调用将解析为名为“GetAllProducts”的路由
对的调用将解析为名为“GetProductById”的路由
然后,您可以在控制器中创建另一个具有相同签名的GetProduct操作,只需适当设置属性路由,例如
[Route("/department/{departmentId}", Name = "GetAllProductsForDepartment")]
public IHttpActionResult GetAllProductsWithDeptId(int locationid, int departmentId)
{
IList<Product> products = this.GetProducts(locationid, departmentId);
[Route(“/department/{departmentId}”,Name=“GetAllProductsForDepartment”)]
公共IHttpActionResult GetAllProductsWithDeptId(内部位置ID,内部部门ID)
{
IList products=this.GetProducts(locationid,departmentId);
现在,对的调用将解析为名为“GetAllProductsFordPartment”的路由
我知道这个示例使用的是Web Api 2,但请查看Web Api中的属性路由。它应该完全相同,而您将返回的不是IHttpActionResult。我知道查询字符串参数与路由无关。假设您希望EmployeesController中的另一个“Get”操作具有相同的参数meters,类似于以下内容:public IEnumerable Get(int DepartmentId=1){….}。如何配置此操作方法的路由?如果希望使用不同的参数(根据示例使用不同的名称)从同一url段读取时,您需要创建两个不同的控制器。如果需要在同一控制器中创建两个get,请为每个get分配特定操作,但它们不能从同一url段读取相同的不同参数名称:您可能至少需要其中一个的querystring,特别是“DepartmentId”因为控制员被命名为“雇员”,所以更有意义
[Route("{productid}", Name = "GetProductById")]
public IHttpActionResult GetProduct(int locationid, string productid)
{
Product product = this.GetProductByID(locationid, productid);
[Route("/department/{departmentId}", Name = "GetAllProductsForDepartment")]
public IHttpActionResult GetAllProductsWithDeptId(int locationid, int departmentId)
{
IList<Product> products = this.GetProducts(locationid, departmentId);