使用 WebInvoke 在 WCF WebApi 中 POST 数据

发布于 2024-11-19 19:27:15 字数 2722 浏览 3 评论 0原文

我最近开始使用 WCF WebApi 创建 REST api。我遵循了 CodePlex 上提供的示例以及 文章系列,作者:Alex Zeitler。

我尝试创建一个通过 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 标头字符。 参数名称:name'.有关更多详细信息,请参阅服务器日志。例外情况 堆栈跟踪是:

at System.Net.WebHeaderCollection.CheckBadChars(字符串名称,布尔值 isHeaderValue) 在 System.Net.WebHeaderCollection.Add(字符串名称, 字符串值)位于 System.Collections.Specialized.NameValueCollection.Add(NameValueCollection 猫 System.ServiceModel.Activation.HostedHttpContext.HostedRequestContainer.System.ServiceModel.Channels.HttpRequestMessageProperty.IHttpHeaderProvider.CopyHeaders(WebHeaderCollection 标题)位于 System.ServiceModel.Channels.HttpRequestMessageProperty.get_Headers() 在 Microsoft.ApplicationServer.Http.Channels.HttpMessageEncodingRequestContext.ConfigureRequestMessage(消息 消息)中 F:\ codeplex \ wcf \ Http \ Src \ Microsoft.ApplicationServer.Http \ Microsoft \ ApplicationServer \ Http \ Channels \ HttpMessageEncodingRequestContext.cs:行 222 在 Microsoft.ApplicationServer.Http.Channels.HttpMessageEncodingRequestContext.get_RequestMessage() 在 F:\ codeplex \ wcf \ Http \ Src \ Microsoft.ApplicationServer.Http \ Microsoft \ ApplicationServer \ Http \ Channels \ HttpMessageEncodingRequestContext.cs:行 54 于 System.ServiceModel.Dispatcher.ChannelHandler.EnsureChannelAndEndpoint(RequestContext 请求)在 System.ServiceModel.Dispatcher.ChannelHandler.TryRetreeringInstanceContext(RequestContext 请求)

知道为什么会发生这种情况吗?

I've recently started using WCF WebApi to create a REST api. I followed samples available on CodePlex and also articles series by Alex Zeitler.

I tried to create a method which accepts data via POST as follows:

[ServiceContract]
public class AuthenticateApi
{
    [WebInvoke(UriTemplate = "", Method = "POST")]  
    public HttpResponseMessage<LoginModel> Post(LoginModel loginModel)
    {
        loginModel.IsValidated = true;
        return new HttpResponseMessage<LoginModel>(loginModel);
    }
}

And this is my Entity:

public class LoginModel
{
    public string Username { get; set; }
    public string Password { get; set; }
    public bool IsValidated { get; set; }
}

And finally this is my configuration in Global.asax:

public static void RegisterRoutes(RouteCollection routes)
{
   routes.MapServiceRoute<AuthenticateApi>("login");
}
protected void Application_Start(object sender, EventArgs e)
{
   RegisterRoutes(RouteTable.Routes);
}

When I try to POST something using Fiddler this way:

Content-Type: application/json
Accept: application/json
{"Username": "mahdi", "Password":"123"}
Host: localhost:8181

I receive the following error message:

The server encountered an error processing the request. The exception
message is 'Specified value has invalid HTTP Header characters.
Parameter name: name'. See server logs for more details. The exception
stack trace is:

at System.Net.WebHeaderCollection.CheckBadChars(String name, Boolean
isHeaderValue) at System.Net.WebHeaderCollection.Add(String name,
String value) at
System.Collections.Specialized.NameValueCollection.Add(NameValueCollection
c) at
System.ServiceModel.Activation.HostedHttpContext.HostedRequestContainer.System.ServiceModel.Channels.HttpRequestMessageProperty.IHttpHeaderProvider.CopyHeaders(WebHeaderCollection
headers) at
System.ServiceModel.Channels.HttpRequestMessageProperty.get_Headers()
at
Microsoft.ApplicationServer.Http.Channels.HttpMessageEncodingRequestContext.ConfigureRequestMessage(Message
message) in
F:\codeplex\wcf\Http\Src\Microsoft.ApplicationServer.Http\Microsoft\ApplicationServer\Http\Channels\HttpMessageEncodingRequestContext.cs:line
222 at
Microsoft.ApplicationServer.Http.Channels.HttpMessageEncodingRequestContext.get_RequestMessage()
in
F:\codeplex\wcf\Http\Src\Microsoft.ApplicationServer.Http\Microsoft\ApplicationServer\Http\Channels\HttpMessageEncodingRequestContext.cs:line
54 at
System.ServiceModel.Dispatcher.ChannelHandler.EnsureChannelAndEndpoint(RequestContext
request) at
System.ServiceModel.Dispatcher.ChannelHandler.TryRetrievingInstanceContext(RequestContext
request)

Any idea why this happens?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅沫记忆 2024-11-26 19:27:15

将 JSON 对象放在请求正文字段中,而不是放在标头中。

Put the JSON object in the request body field, not in with the headers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文