我最近开始使用WCF WebApi来创建REST。我遵循了亚历克斯·齐特勒( Alex )在CodePlex和文章系列上提供的示例。
我试图创建一个通过POST接受数据的方法,如下所示:
[ServiceContract]
public class AuthenticateApi
{
[WebInvoke(UriTemplate = "", Method = "POST")]
public HttpResponseMessage<LoginModel> Post(LoginModel loginModel)
{
loginModel.IsValidated = true;
return new HttpResponseMessage<LoginModel>(loginModel);
}
}这是我的实体:
public class LoginModel
{
public string Username { get; set; }
public string Password { get; set; }
public bool IsValidated { get; set; }
}最后,这是我在Global.asax中的配置:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapServiceRoute<AuthenticateApi>("login");
}
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}当我试图以这种方式使用Fiddler发布一些东西时:
Content-Type: application/json
Accept: application/json
{"Username": "mahdi", "Password":"123"}
Host: localhost:8181我收到以下错误消息:
服务器在处理请求时遇到错误。异常消息是“指定的值具有无效的HTTP头字符。参数名称:名称”。有关详细信息,请参阅服务器日志。异常堆栈跟踪是: 在System.Net.WebHeaderCollection.CheckBadChars(String名称,布尔isHeaderValue在System.Net.WebHeaderCollection.Add(字符串名,(字符串值)(在System.Collections.Specialized.NameValueCollection.Add(NameValueCollection c)在System.ServiceModel.Activation.HostedHttpContext.HostedRequestContainer.System.ServiceModel.Channels.HttpRequestMessageProperty.IHttpHeaderProvider.CopyHeaders(WebHeaderCollection头(在System.ServiceModel.Channels.HttpRequestMessageProperty.get_Headers() at Microsoft.ApplicationServer.Http.Channels.HttpMessageEncodingRequestContext.ConfigureRequestMessage(Message message) )在F:\codeplex\wcf\Http\Src\Microsoft.ApplicationServer.Http\Microsoft\ApplicationServer\Http\Channels\HttpMessageEncodingRequestContext.cs:line 222 (微软)。ApplicationServer.Http.Channels.HttpMessageEncodingRequestContext.get_RequestMessage() in F:\codeplex\wcf\Http\Src\Microsoft.ApplicationServer.Http\Microsoft\ApplicationServer\Http\Channels\HttpMessageEncodingRequestContext.cs:line 54 (应System.ServiceModel.Dispatcher.ChannelHandler.EnsureChannelAndEndpoint(RequestContext请求)
知道为什么会这样吗?
发布于 2011-07-10 12:37:20
将JSON对象放在request字段中,而不是在headers中。
https://stackoverflow.com/questions/6640869
复制相似问题