[英]Multiple routing with same paths but with different complex parameters
For example: 例如:
api/file/occurrence?sha256=...
[HttpGet]
[Route("api/file/")]
public async Task<IHttpActionResult> GetFileBySha256Async([FromUri] FilesBySha256RequestDTO requestDTO)
{
}
api/file/occurrence?sha256=...&from_date=..&to_date=..
[HttpGet]
[Route("api/file/")]
public async Task<IHttpActionResult> GetFileBySha256AndDateAsync([FromUri] FilesBySha256AndDateRequestDTO requestDTO)
{
}
And the DTOs: 和DTO:
public class FilesBySha256RequestDTO
{
public string sha256 { get; set; }
}
public class FilesBySha256AndDateRequestDTO
{
public string sha256 { get; set; }
public DateTime? from_date { get; set; }
public DateTime? to_date { get; set; }
}
How can I accomplish this behavior? 我怎样才能做到这一点? I am getting the following exception: 我收到以下异常:
"ExceptionMessage": "Multiple actions were found that match the request: \\r\\nGetFileBySha256Async on type Cynet.Client.WebAPI.Controllers.FileController\\r\\nGetFileOccurrencesSha256 on type Cynet.Client.WebAPI.Controllers.FileController
It is not possible to distinguish the route between two because api/file/occurrence?sha256=...&from_date=..&to_date=..
and api/file/occurrence?sha256=...
is the same thing for the framework. 无法区分两者之间的路由,因为api/file/occurrence?sha256=...&from_date=..&to_date=..
和api/file/occurrence?sha256=...
对于框架是同一回事。 The first thing you can do is changing the second route like api/fileOnDate/
. 您可以做的第一件事就是更改第二条路由,例如api/fileOnDate/
。 If it is impossible to do it, you can define a third function and use it as a manual router such as; 如果无法实现,则可以定义第三个功能,并将其用作手动路由器,例如;
[HttpGet]
[Route("api/file/")]
public async Task<IHttpActionResult> GetFileBy([FromUri] FilesBySha256AndDateRequestDTO requestDTO)
{
if (!requestDTO.from_date.HasValue && !requestDTO.to_date.HasValue)
{
return await this.GetFileBySha256Async(new FilesBySha256RequestDTO() { sha256 = requestDTO.sha256 });
}
else
{
return await this.GetFileBySha256AndDateAsync(requestDTO);
}
}
private async Task<IHttpActionResult> GetFileBySha256Async(FilesBySha256RequestDTO requestDTO)
{
}
private async Task<IHttpActionResult> GetFileBySha256AndDateAsync(FilesBySha256AndDateRequestDTO requestDTO)
{
}
hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.