简体   繁体   English

如何为webapi配置此路由?

[英]how to configure this route for webapi?

I have a controller to GET a resource(say, Employee), which has two properties (say, CategoryId and DepartmentId). 我有一个控制器来获取资源(例如,Employee),它具有两个属性(例如,CategoryId和DepartmentId)。 I need to configure the routes to support the following URLs: 我需要配置路由以支持以下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 departmentId=1]

and the controller code looks like this: 控制器代码如下所示:

public IEnumerable<Employee> Get()
{
    ....
}

public IEnumerable<Employee> Get(int employeeId, int departmentId = -1, int categoryId = -1)
{
    .....
}

How to configure routes for this controller? 如何为该控制器配置路由?

Thanks 谢谢

for any querystyring parameter, there's nothin to do on the routing side: just have the controller method parmater matching with the qs parameter name(case insensitive.) 对于任何querystyring参数,路由侧都没有要做:只需将控制器方法parmater与qs参数名称匹配(不区分大小写)。

If instead your method parameter refers to a uri segment, method paramenter name has to match with route parameter/segment between curly brackets 如果相反,您的方法参数引用了uri段,则方法参数的名称必须与大括号之间的route参数/段匹配

routes.MapHttpRoute(
    name: "API Default",
    routeTemplate: "/api/{controller}/{employeeId}",
    defaults: new { id = RouteParameter.Optional }
);

that means you need a controller with following method 这意味着您需要使用以下方法的控制器

public class EmployeesController : ApiController
{
public IEnumerable<Employee> Get(int employeeId){...} 
}

keep in mind that, unless you use action, on your controller you can only one method per http verb. 请记住,除非使用操作,否则在控制器上,每个http动词只能使用一种方法。
In other word your sample having 2 method for the verb get won't work unless you use explicit action for both of them. 换句话说,除非对两个动词都使用显式操作,否则对动词get具有2方法的示例将不起作用。

Have you looked at using Attribute routing? 您是否看过使用属性路由? We use Attribute routing extensively now, to the point that we have completely got rid of the default /controller/action type routes that we had with MapHttpRoute. 现在,我们广泛使用属性路由,以至于我们已经完全摆脱了MapHttpRoute使用的默认/ controller / action类型路由。

Instead we decorate our controllers as follows. 相反,我们按如下方式装饰控制器。 Firstly we create a route prefix for the controller so that we know what the base route is that we require 首先,我们为控制器创建一个路由前缀,以便我们知道我们需要的基本路由

/// <summary>   A controller for handling products. </summary>
[RoutePrefix("api/purchasing/locations/{locationid}/products")]
public class ProductsController : PurchasingController
{

Then for each action in the controller we decorate it as follows: 然后,对于控制器中的每个动作,我们将其装饰如下:

    [Route("", Name = "GetAllProducts")]
    public IHttpActionResult GetAllProducts(int locationid, ODataQueryOptions<FourthPurchasingAPI.Models.Product> queryOptions)
    {
        IList<Product> products = this.GetProducts(locationid);

and

    [Route("{productid}", Name = "GetProductById")]
    public IHttpActionResult GetProduct(int locationid, string productid)
    {
        Product product = this.GetProductByID(locationid, productid);

so a call to api/purchasing/locations/1/products/ will resolve to the route named "GetAllProducts" and a call to api/purchasing/locations/1/products/1 will resolve to the route named "GetProductById" 因此,对api / purchasing / locations / 1 / products /的调用将解析为名为“ GetAllProducts”的路由,而对api / purchasing / locations / 1 / products / 1的调用将解析为名为“ GetProductById”的路由

You could then create another GetProduct action in your controller with the same signature, just set the attribute route appropriately, eg 然后,您可以在控制器中使用相同的签名创建另一个GetProduct操作,只需适当地设置属性路由即可,例如

    [Route("/department/{departmentId}", Name = "GetAllProductsForDepartment")]
    public IHttpActionResult GetAllProductsWithDeptId(int locationid, int departmentId)
    {
        IList<Product> products = this.GetProducts(locationid, departmentId);

Now a call to api/purchasing/locations/1/products/department/1234 will resolve to the route named "GetAllProductsForDepartment" 现在,对api / purchasing / locations / 1 / products / department / 1234的调用将解析为名为“ GetAllProductsForDepartment”的路由

I know that this example is using Web Api 2, but look at this link for Attribute Routing in Web Api. 我知道该示例使用的是Web Api 2,但请查看此链接以了解Web Api中的属性路由。 It should be exactly the same instead you will be returning something other than an IHttpActionResult. 它应该完全相同,否则您将返回IHttpActionResult以外的其他内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM