API路由:带路径的多个操作

时间:2018-03-01 18:40:57

标签: c# asp.net-web-api swagger asp.net-web-api-routing

我正在尝试配置我的API路线,而我似乎无法摆脱Swagger的错误:

  

500:{"消息":"发生错误。"," ExceptionMessage":" Swagger 2.0不支持:多个操作路径' api /门/ {OrganizationSys}'和方法' GET'。

我理解为什么会收到错误,但我不确定如何修复错误。以下是API端点:

public IHttpActionResult Get(int organizationSys)
{
    ....
}

public IHttpActionResult Get(int organizationSys, int id)
{
    ....
}


public IHttpActionResult Post([FromBody]Doors door)
{
    ....
}

public IHttpActionResult Put([FromBody]Doors door)
{
    ....
}

public IHttpActionResult Delete(int organizationSys, int id)
{
    ....
}

这是我的路线,显然不正确:

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

config.Routes.MapHttpRoute(
    name: "route2",
    routeTemplate: "api/{controller}/{organizationSys}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

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

更新

我现在有这个,但得到同样的错误:

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

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

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

更新2:这就是我现在拥有的......

靠近,我想。

[Route("api/Doors/{organizaitonSys}")]
public IHttpActionResult Get(int organizationSys)
{
    ....
}

[Route("api/Doors/{organizaitonSys}/{id}")]
public IHttpActionResult Get(int organizationSys, int id)
{
    ....
}

[Route("api/Doors")]
public IHttpActionResult Post([FromBody]Doors door)
{
    ....
}

[Route("api/Doors")]
public IHttpActionResult Post([FromBody]Doors door)
{
    ....
}

[Route("api/Doors/{organizaitonSys}/{id}")]
public IHttpActionResult Delete(int organizationSys, int id)
{
    ....
}

然后路线:

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

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

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

但是,当我尝试拨打

GET: http://localhost:26307/api/Doors/1012

GET: http://localhost:26307/api/Doors/1012/23

DELETE: http://localhost:26307/api/Doors/1012/23

我收到404错误。现在在Swagger中,organizationSys参数在Try It部分显示两次:

enter image description here

1 个答案:

答案 0 :(得分:5)

由于可选route1

route2{id}具有相同的映射。删除可选参数并将route2放在route1之前,因为route1更为通用。

您还可以提供约束,这些约束限制URI段与占位符匹配的方式:

constraints: new { id = @"\d+" }   // Only matches if "id" is one or more digits.

实施例

config.Routes.MapHttpRoute(
    name: "route2",
    routeTemplate: "api/{controller}/{organizationSys}/{id}",
    constraints: new { id = @"\d+" }   // Only matches if "id" is one or more 
);

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

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

还有一个问题映射到

public IHttpActionResult Get(int OrganizationSys, int DoorID) {
    ....
}

因为DoorID参数,它与路径模板的参数不匹配。将其重命名为id或更新模板以匹配。

public IHttpActionResult Get(int organizationSys, int id) {
    ....
}

如果使用属性路由,请确保在基于约定的路由

之前启用它
public static class WebApiConfig {
    public static void Register(HttpConfiguration config) {
        // Attribute routing.
        config.MapHttpAttributeRoutes();

        // Convention-based routing.

       //...code removed for brevity
    }
}

然后通过属性将路径应用到控制器。

[RoutePrefix("api/Doors")]
public class DoorsController : ApiController {

    //matches GET api/doors/5
    [HttpGet]
    [Route("{organizationSys:int}")]
    public IHttpActionResult Get(int organizationSys) {
        //....
    }

    //matches GET api/doors/5/1
    [HttpGet]
    [Route("{organizationSys:int}/{id:int}")]
    public IHttpActionResult Get(int organizationSys, int id) {
        ....
    }    

    //matches POST api/doors
    [HttpPost]
    [Route("")]
    public IHttpActionResult Post([FromBody]Doors door) {
        //....
    }

    //matches PUT api/doors
    [HttpPut]
    [Route("")]    
    public IHttpActionResult Put([FromBody]Doors door) {
        //....
    }

    //matches DELETE api/doors/5/1
    [HttpDelete]
    [Route("{organizationSys:int}/{id:int}")]    
    public IHttpActionResult Delete(int organizationSys, int id) {
        //....
    }    
}

请注意使用RoutePrefix属性来减少重复的模板部件和路径约束。

参考Attribute Routing in ASP.NET Web API 2