【发布时间】:2017-12-21 04:08:29
【问题描述】:
我正在尝试了解 Web-Api 如何解析路由。我有两条使用相同基本路径的路线
[WriteRoute(DivisionAPIRoutes.PAYROLL_IMPORT_PTO)]
[HttpPost]
public void ImportPTOByIds(GlobalEntityKey<IDivision> parentId, GlobalEntityKey<IDivisionPayCalendar> id,
ImportPTORequestDTO importPTORequest, [FromUri] GlobalEntityKey<IPTORequest>[] ptoRequestIds)
{
GlobalFactory<IEmployeePTOListService>.Instance.ImportPTOByIds(parentId, id, ptoRequestIds, importPTORequest);
}
[WriteRoute(DivisionAPIRoutes.PAYROLL_IMPORT_PTO)]
[HttpPost]
public void ImportPTOByFilter(GlobalEntityKey<IDivision> parentId, GlobalEntityKey<IDivisionPayCalendar> id, ImportPTORequestDTO importPTORequest, string filterOptions,
[FromUri] GlobalEntityKey<IPTORequest>[] excludedPTORequestIds)
{
var filterOptionsDTO = JsonConvert.DeserializeObject<FilterOptionsDTO>(filterOptions);
GlobalFactory<IEmployeePTOListService>.Instance.ImportPTOByFilter(parentId, id, filterOptionsDTO, excludedPTORequestIds, importPTORequest);
}
注意:默认情况下,构建 CLR 类型 string、int 是从 URI 中检索的
我正在发出一个工作正常的发布请求,但我试图了解路由如何解析为方法的底层逻辑:
(为了方便解码)
https://localhost/api/paystream/v1/divisions/1af4edea-d442-4fda-b29d-02c42951c0d0/payrolls/cd2ed43d-0f3d-48fb-8d00-15294a8fa06e/_actions/import-pto?filterOptions={"query":"","filterParameters":[{"fieldName":"RequestStartDate","parsebleValue":"2016-01-01","filterType":"GreaterThanOrEqual"},{ "fieldName":"RequestStartDate","parsebleValue":"2017-12-31","filterType":"LessThanOrEqual"}]}
帖子正文:{ AlwaysCreateNewCheck:假, PayBatchType:'支票', PayBatchId:'1903771' }
如果我从请求中省略了排除的 PTORequestIds。这仍将解析为ImportPTOByFilter,但如果我包含排除的PTORequestIds 并省略filterOptions,则ImportPTOByIds 被选中。
我倾向于认为由模型绑定器处理的列表和数组与模型绑定的其他默认 CLR 类型(字符串、int、Guid 等)具有不同的行为。
虽然字符串是必需的并且会抛出 404 或解析到其他路由,但不需要在请求中明确定义数组。
可以安全地假设,从 WebApi 进行路由解析的其他规则是什么。
【问题讨论】:
标签: c# arrays asp.net-web-api custom-model-binder