我正在使用Kendo AutoComplete客户端javascript小部件,它发送服务器请求,如下所示: https://domainName/Proto2/api/Goal/Lookup?text=ABC&goalId=8b625c56-7b04-4281-936f-b88d7ca27d76&filter%5Blogic%5D=and&filter%5Bfilters%5D%5B0%5D%5Bvalue%5D=&filter%5Bfilters%5D%5B0%5D%5Boperator%5D=contains&filter%5Bfilters%5D%5B0%5D%5Bfield%5D=Description&filter%5Bfilters%5D%5B0%5D%5BignoreCase%5D=true&_=1423833493290
接收它的MVC服务器端方法是:
[Route("api/Goal/Lookup")]
[HttpGet] // if the action name doesn't start with "Get", then we need to specify this attribute
public ICollection<IAllegroGoalContract> Lookup(Guid goalId, string text = "")
如果客户端为text参数发送空值,则会出现此问题(例如:text =&amp; goalId = 8b625c56-7b04-4281-936f-b88d7ca27d76)。在这种情况下.net返回以下错误。
我尝试了各种路线属性值:
[Route("api/Goal/Lookup/{goalId:guid},{text?}")]
[Route("api/Goal/Lookup/{text?}")]
答案 0 :(得分:0)
看起来你的参数被用作过滤器,所以不是将GoalId和Text参数作为路径的一部分,而是定义一个这样的类:
public class LookupOptions
{
public Guid GoalId { get; set; } // change this to Guid? if the client can send a nullable goalId.
public string Text { get; set; }
}
所以你的方法签名将是:
[Route("api/Goal/Lookup")]
[HttpGet]
public ICollection<IAllegroGoalContract> Lookup([FromUri]LookupOptions options)
{
// Note that [FromUri] will allow the mapping of the querystring into LookupOptions class.
}
现在,您可以从客户端传递您的选项作为查询字符串的一部分,它将被分配给LookupOptions参数。
希望这有帮助。