【问题标题】:WCF/WebAPI: HTTP 500 error when I pass more than one parameter to a WebGet methodWCF/WebAPI:当我将多个参数传递给 WebGet 方法时出现 HTTP 500 错误
【发布时间】:2011-10-05 01:13:20
【问题描述】:

我有一个非常愚蠢的问题,但由于某种原因,我无法在网上找到任何解决方法或相关信息。

总结:我不能将多个参数传递给 WebGet 方法。如果我这样做了,服务器会返回 HTTP 500 错误并且该方法不会执行。我的代码和请求如下。

[ServiceContract]
public class WebmailAPI {
...
[WebGet(UriTemplate= "Webmail?messagetype={messageType}&unreadonly={unreadOnly}&skip={skip}&take={take}")]
    public void Get(MessageType messageType, bool unreadOnly, int skip, int take) {
    ...
    }
 }

全球.ASAX.CS: routes.SetDefaultHttpConfiguration(new WebApiConfiguration() { EnableHelpPage = true, EnableTestClient = true }); RouteTable.Routes.MapServiceRoute("api/");

这个请求执行得很好: http://localhost:9000/api/Webmail/?messagetype=1

这个返回500错误:

http://localhost:9000/api/Webmail/?messagetype=1&unreadonly=0&skip=0&take=100

信息: VS2010 SP1 + ASP.NET 4 + Entity Framework 2001 年 6 月 CTP + 最新的 WebAPI

提前感谢您的帮助!

附:我尝试在查询字符串中对“&”进行 HTML 编码——没有效果。

【问题讨论】:

    标签: wcf wcf-web-api


    【解决方案1】:

    使用false 代替0 代替unreadonly

    http://localhost:9000/api/Webmail/?messagetype=1&unreadonly=false&skip=0&take=100

    请参阅WCF Web HTTP Programming Model Overview,特别是 UriTemplate 查询字符串参数和 URLs 部分,了解按数据类型划分的查询字符串参数格式。

    【讨论】:

    • 是的,你是对的。对于观众:嗯,WCF/WebHttp/WebAPI 对参数类型非常敏感。对于布尔参数,请确保将值指定为真/假,而不是 1/0。这有点好笑,但它虽然可以识别枚举,但无法解析用数字表示的布尔值。
    • @AlexAvrutin:我不认为这很奇怪,框架支持枚举,布尔值也是如此。但是,在 .NET 中,唯一允许的值是“真”和“假”。 '0' 和 '1' 不是布尔值,它们是整数,没有隐式转换。
    猜你喜欢
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 2013-05-29
    • 2018-04-22
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多