API 路线:具有路径的多个操作
API Routes: Multiple operations with path
我正在尝试配置我的 API 路由,但我似乎无法解决来自 Swagger 的这个错误:
500 : {"Message":"An error has occurred.","ExceptionMessage":"Not supported by Swagger 2.0: Multiple operations with path 'api/Doors/{OrganizationSys}' and method '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 部分出现两次:
由于可选的 {id}
,route1
和 route2
具有相同的映射。删除可选参数并将 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) {
....
}
如果使用属性路由,请确保在 convention-based 路由
之前启用它
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
属性来减少重复的模板部分和路线约束。
我正在尝试配置我的 API 路由,但我似乎无法解决来自 Swagger 的这个错误:
500 : {"Message":"An error has occurred.","ExceptionMessage":"Not supported by Swagger 2.0: Multiple operations with path 'api/Doors/{OrganizationSys}' and method '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 部分出现两次:
{id}
,route1
和 route2
具有相同的映射。删除可选参数并将 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) {
....
}
如果使用属性路由,请确保在 convention-based 路由
之前启用它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
属性来减少重复的模板部分和路线约束。