Reach Web Api 操作在查询字符串中包含和号

Reach Web Api Action with ampersand in query string

如何访问我的 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."
}

我以为 Web API 会自动解码查询字符串参数,但显然它不会...

当您在查询字符串中使用 & 时,您实际上是在转义该符号,以便它作为该参数的一部分被读取。所以 Web API 方法实际上只接收一个参数,"user",其中包含 "test&pw=abc123"。