相关疑难解决方法(0)

ASP NET Web API路由模板

我有一个名为Agency的实体跟随api

GET     http://localhost:37331/api/agency?start=1&limit=10&status=1
GET     http://localhost:37331/api/agency/2
POST    http://localhost:37331/api/agency 
PUT     http://localhost:37331/api/agency
DELETE  http://localhost:37331/api/agency/4
POST    http://localhost:37331/api/agency/activate/3
POST    http://localhost:37331/api/agency/deactivate/3
GET     http://localhost:37331/api/agency/types
Run Code Online (Sandbox Code Playgroud)

我使用的路线模板是

        config.Routes.MapHttpRoute(
            name: "ControllerActionIdApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { },
            constraints: new { id = @"\d+" }
        );
        //
        config.Routes.MapHttpRoute(
            name: "ControllerActionApi",
            routeTemplate: "api/{controller}/{action}"
        );
        //
        config.Routes.MapHttpRoute(
            name: "ControllerIdApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { },
            constraints: new { id = @"\d+" }
        );
        //
        config.Routes.MapHttpRoute(
            name: "ControllerApi",
            routeTemplate: "api/{controller}"
        );
Run Code Online (Sandbox Code Playgroud)

显然第二和第三之间存在歧义.我有一个解决方法,将id放入查询字符串

GET     http://localhost:37331/api/agency?id=2
DELETE  http://localhost:37331/api/agency?id=4
Run Code Online (Sandbox Code Playgroud)

我认为一定有聪明的方法.你能就此提出建议吗?

谢谢

asp.net routes asp.net-web-api asp.net-web-api-routing

4
推荐指数
1
解决办法
3320
查看次数