WEB API路由动作多个

时间:2015-03-25 17:39:24

标签: asp.net-web-api

我可以为特定的控制器定义路由吗?

第3条路线,通过某些操作路由到Controller,但路线只能在其他路线之前正常工作。

请一些建议?

        //  ex: api/groups/Collective1/john01
        config.Routes.MapHttpRoute(
            name: ControllerAndCollectiveAndUserID,
            routeTemplate: "api/{controller}/{collective}/{userid}",
            defaults: null, //defaults: new { id = RouteParameter.Optional } 
            constraints: new
            {
                collective = @"^[a-zA-Z\d]+$",
                userid = @"^[a-zA-Z\d]+$"
            }
        ); 

        //  ex: api/groups/Collective1
        config.Routes.MapHttpRoute(
            name: ControllerAndCollective,
            routeTemplate: "api/{controller}/{collective}",
            defaults: null, //defaults: new { id = RouteParameter.Optional } 
            constraints: new
            {
                collective = @"^[a-zA-Z\d]+$"
            }
        );            

        // ex: api/users/pending
        config.Routes.MapHttpRoute(
            name: ControllerAction,
            routeTemplate: "api/{controller}/{action}"
        );

        // ex: api/persons
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
           name: ControllerOnly,
           routeTemplate: "api/{controller}"
        );

0 个答案:

没有答案