Web API 2中具有MVC 5 AttributeRouting的QueryString [英] QueryString with MVC 5 AttributeRouting in Web API 2
本文介绍了Web API 2中具有MVC 5 AttributeRouting的QueryString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码
[HttpGet]
[Route("publish/{id}")]
public IHttpActionResult B(string id, string publishid=null) { ... }
据我了解,
~/..../publish/1?publishid=12
~/..../publish?id=1&publishid=12
应该工作并绑定两个参数,但在第二种情况下将不工作. 在第一种情况下,publishid将不受约束.
Should work and bind both parameters but it won't work on the second case. In the first case, publishid will not be bound.
所以我不明白为什么这不起作用.知道为什么会这样吗?
So I do not understand why this is not working. Any idea why it is in this way?
推荐答案
第二种情况不起作用,因为id
是路由模板publish/{id}
中的必需变量.在Web API中,首先进行路由模板匹配,然后进行操作选择过程.
The second case will not work because id
is a required variable in the route template publish/{id}
. In Web API first route template matching happens and then the action selection process.
其他情况:
-
publish/1
-将不起作用,因为操作B
表示需要publishid.为防止这种情况,您可以将动作签名更改为B(string id, string publishid=null)
之类,并且仅绑定id
-
publish/1?publishid=10
-两者均受约束的地方正常工作.
publish/1
- will not work as actionB
is saying that publishid is required. To prevent this you can change the signature of action to be something likeB(string id, string publishid=null)
and onlyid
is boundpublish/1?publishid=10
- works as expected where both are bound.
这篇关于Web API 2中具有MVC 5 AttributeRouting的QueryString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文