获取参数字典的C#WebApi包含空条目错误

获取参数字典的C#WebApi包含空条目错误,c#,c#-4.0,asp.net-web-api2,asp.net-web-api-routing,C#,C# 4.0,Asp.net Web Api2,Asp.net Web Api Routing,使用以下api方法: [HttpPut] [Route("Customers/{CustomerId}/Search", Name = "CustomerSearch")] [ResponseType(typeof(SearchResults))] public async Task<IHttpActionResult> Search([FromBody]SearchFilters filters, long? CustomerId = null) { //This func

使用以下api方法:

[HttpPut]
[Route("Customers/{CustomerId}/Search", Name = "CustomerSearch")]
[ResponseType(typeof(SearchResults))]
public async Task<IHttpActionResult> Search([FromBody]SearchFilters filters, long? CustomerId = null)
{
    //This func searches for some subentity inside customers
}

有人能帮助解决这个问题吗?或者纠正我做错了什么?

可选参数应该用作模板的结尾,因为它们可以从url中排除

此外,通过对客户id使用路由约束,您将确保不会将关键字误认为客户id

参考:

//放置客户/10/搜索
[HttpPut]
[Route(“Customers/{CustomerId:long}/Search”,Name=“CustomerSearch”)]
[响应类型(类型(搜索结果))]
公共异步任务搜索(长CustomerId,[FromBody]SearchFilters,){
//此函数搜索客户内部的某些子实体
}
//获取客户/搜索
//获取客户/搜索/关键字
[HttpGet]
[路由(“客户/Search/{keyword?}”,Name=“GetCustomerByKeyword”)]
公共异步任务SearchCustomers(字符串关键字=“”){
//此函数根据客户名称中的关键字搜索客户
}

谢谢。在我的情况下,CustomerId应该是长的。它是否适用于nullable long?否,因为在可选参数之后不能有段。
[HttpGet]
[Route("Customers/Search/{keyword}", Name = "GetCustomersByKeyword")]
public async Task<IHttpActionResult> SearchCustomers(string keyword = "")
{
    //This func searches for customers based on the keyword in the customer name
}
//PUT Customers/10/Search
[HttpPut]
[Route("Customers/{CustomerId:long}/Search", Name = "CustomerSearch")]
[ResponseType(typeof(SearchResults))]
public async Task<IHttpActionResult> Search(long CustomerId, [FromBody]SearchFilters filters, ) {
    //This func searches for some subentity inside customers
}

//GET Customers/Search    
//GET Customers/Search/keyword
[HttpGet]
[Route("Customers/Search/{keyword?}", Name = "GetCustomersByKeyword")]
public async Task<IHttpActionResult> SearchCustomers(string keyword = "") {
    //This func searches for customers based on the keyword in the customer name
}