简体   繁体   English

具有相同路径但具有不同复杂参数的多个路由

[英]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.

相关问题 ServiceStack多个路由路径 - ServiceStack multiple routing paths 多参数属性路由 - Attribute routing with multiple parameters 多个可选参数路由 - Multiple optional parameters routing 多个后台工作人员使用不同的参数调用同一函数 - Multiple backgroundworkers calling same function with different parameters 使用不同的参数多次运行同一查询 - Running the same query multiple times with different parameters WEB API路由错误找到了多个与不同API路径的请求匹配的操作 - WEB API Routing Error Multiple actions were found that match the request with different API paths 具有多个参数的 Web API 路由 - Web API routing with multiple parameters Web API 2 / MVC 5:属性路由将参数作为查询字符串传递,以在同一控制器上定位不同的操作 - Web API 2 / MVC 5 : Attribute Routing passing parameters as querystring to target different actions on same controller 实体框架 - 同一实体的不同代理对象。 并包含具有到同一目的地的多条路径的行为 - Entity Framework - Different proxy objects for the same entity. And Include behavior with multiple paths to same destination 具有多个可选参数的 Asp.net 核心路由调用不同的操作 - Asp.net core Routing with multiple optional parameters calling different action
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM