ASP.NET Web API multiple RoutePrefix

为君一笑 提交于 2019-12-04 15:59:59

问题


The opensource Attribute Routing allows to have multiple route-prefixes. Why does ASP.NET Web API 2.0 does not allow to have multiple RoutePrefix().

[RoutePrefix("api/v1/{abc}/Entity")]
[RoutePrefix("api/v1/{abc}/{xyz?}/Entity")]
public class MyApiController : ApiController
{
   [Route("")]
   public IHttpResult Get()
   {
      return Ok("Hello World");
   }
}

回答1:


You can add a route to the action method also overriding the RoutePrefix with a "~"

example:

[RoutePrefix("api/v1/{abc}/Entity")]
public class MyApiController : ApiController
{
   [Route("")]
   [Route("~/api/v1/{abc}/{xyz?}/Entity")]
   public IHttpResult Get()
   {
      return Ok("Hello World");
   }
}

Notice the line: [Route("~/ api/v1/{abc}/{xyz?}/Entity")]



来源:https://stackoverflow.com/questions/24953660/asp-net-web-api-multiple-routeprefix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!