首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用WebInvoke在WCF WebApi中发布数据

使用WebInvoke在WCF WebApi中发布数据
EN

Stack Overflow用户
提问于 2011-07-10 11:42:32
回答 1查看 3.9K关注 0票数 1

我最近开始使用WCF WebApi来创建REST。我遵循了亚历克斯·齐特勒( Alex )在CodePlex和文章系列上提供的示例。

我试图创建一个通过POST接受数据的方法,如下所示:

代码语言:javascript
复制
[ServiceContract]
public class AuthenticateApi
{
    [WebInvoke(UriTemplate = "", Method = "POST")]  
    public HttpResponseMessage<LoginModel> Post(LoginModel loginModel)
    {
        loginModel.IsValidated = true;
        return new HttpResponseMessage<LoginModel>(loginModel);
    }
}

这是我的实体:

代码语言:javascript
复制
public class LoginModel
{
    public string Username { get; set; }
    public string Password { get; set; }
    public bool IsValidated { get; set; }
}

最后,这是我在Global.asax中的配置:

代码语言:javascript
复制
public static void RegisterRoutes(RouteCollection routes)
{
   routes.MapServiceRoute<AuthenticateApi>("login");
}
protected void Application_Start(object sender, EventArgs e)
{
   RegisterRoutes(RouteTable.Routes);
}

当我试图以这种方式使用Fiddler发布一些东西时:

代码语言:javascript
复制
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请求)

知道为什么会这样吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-10 12:37:20

将JSON对象放在request字段中,而不是在headers中。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6640869

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档