2016-09-27 68 views
0

如何使用&符号查询字符串中的Web Api操作?在查询字符串中使用与符号实现Web Api操作

这工作:

http://localhost:12345/api/MyController/MyAction?user=test&pw=abc123 

这不:

http://localhost:12345/api/MyController/MyAction?user=test&pw=abc123 

错误消息:

{ 
    "message": "No HTTP resource was found that matches the request URI 'http://localhost:12345/api/MyController/MyAction?user=test&pw=abc123'.", 
    "messageDetail": "No action was found on the controller 'MyController' that matches the request." 
} 

我认为网页API将查询字符串参数自动解码,但apperently它不...

回答

1

在查询字符串中使用&时,实际上是转义该&符号,以便它作为该参数的一部分进行读取。因此,Web API方法实际上只接收一个参数,“用户”具有“test & pw = abc123”。

+0

这对我来说很明显。关键是我怎样才能使它与&符合使用?该URL出现在XML文件中,所以我必须将其转义。 – iappwebdev

+0

HTTP规范不会允许您以这种方式发送它,并将其解释为两个查询字符串变量。你的选择是1)在发送之前将它缩小或2)让你的API只接受“用户”,然后在C#中解析它。 #2似乎很无礼 - #1会更有意义。由于我不确定在调用API的代码中使用哪种语言,因此我不知道如何提示如何忽略它。 – grayimp

+0

好吧,我会和#1一起去,谢谢! – iappwebdev