【发布时间】:2019-09-21 05:33:26
【问题描述】:
我正在使用 ASP.NET Web API。我想 REST uri 是
GET /api/v1/documents/1234/download 或
GET /api/v1/documents/1234?act=download 或
GET /api/v1/documents?id=1234&act=download
是否可以有多种方式调用 REST API Url?推荐吗?
我只使用Attribute Routes
[RoutePrefix("api/v1")]
public class DocumentController : ApiController
{
private readonly DomainService _domainService;
public DocumentController(DomainService domainService)
: base(domainService)
{
_domainService = domainService ?? throw new ArgumentNullException(nameof(domainService));
}
[HttpGet]
[Route("documents/{id:int}")]
public async Task<IHttpActionResult> DownloadDocument([FromUri]int id, [FromUri]string act)
{
if (string.IsNullOrEmpty(act) || act.ToUpper() != "DOWNLOAD")
{
return BadRequest("Invalid action parameter.");
}
return await service.DownloadFile(id);
}
}
上面的代码只有GET /api/v1/documents/1234?act=download 有效。是否可以以所有 3 个路由都调用相同的操作方法的方式配置路由?
【问题讨论】:
标签: rest asp.net-web-api asp.net-web-api2 asp.net-web-api-routing