【发布时间】:2016-07-15 16:39:28
【问题描述】:
我正在尝试在 ASP.Net Web Api 2 中创建以下结构
https://<host>/api/webhooks/incoming/custom
我可以通过创建控制器 webhook 导航到 webhook,但是如何在它下面创建另外两个?
请问有什么想法吗?
【问题讨论】:
标签: asp.net-mvc asp.net-web-api asp.net-web-api2
我正在尝试在 ASP.Net Web Api 2 中创建以下结构
https://<host>/api/webhooks/incoming/custom
我可以通过创建控制器 webhook 导航到 webhook,但是如何在它下面创建另外两个?
请问有什么想法吗?
【问题讨论】:
标签: asp.net-mvc asp.net-web-api asp.net-web-api2
你可以通过属性路由来做到这一点。
在控制器中,您可以将 RoutePrefix 添加到控制器,然后直接在方法上指定每个附加路由。那么控制器内部的所有方法路由都将以api/webhooks/incoming开头。拨打GetStarted()的路线将是api/webhooks/incoming/custom
[RoutePrefix("api/webhooks/incoming")]
public class StartUpController : ApiController
{
[HttpGet]
[Route("custom")]
[AllowAnonymous]
public IHttpActionResult GetStarted()
{
return Ok();
}
}
或者你可以直接在方法上指定完整的路由。路线也将是api/webhooks/incoming/custom
public class StartUpController : ApiController
{
[HttpGet]
[Route("api/webhooks/incoming/custom")]
[AllowAnonymous]
public IHttpActionResult GetStarted()
{
return Ok();
}
}
您可以阅读更多关于它的信息here
【讨论】: