最近、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 を使用して何かを POST しようとすると:
Content-Type: application/json
Accept: application/json
{"Username": "mahdi", "Password":"123"}
Host: localhost:8181
次のエラー メッセージが表示されます。
サーバーでリクエストの処理中にエラーが発生しました。例外メッセージは、「指定された値に無効な HTTP ヘッダー文字が含まれています。パラメータ名: name'. 詳細については、サーバー ログを参照してください。例外スタック トレースは次のとおりです。
System.Net.WebHeaderCollection.CheckBadChars (文字列名、ブール値 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() で Microsoft.ApplicationServer.Http.Channels.HttpMessageEncodingRequestContext.ConfigureRequestMessage(メッセージ メッセージ) in F :\codeplex\wcf\Http\Src\Microsoft.ApplicationServer.Http\Microsoft\ApplicationServer\Http\Channels\HttpMessageEncodingRequestContext.cs:Microsoft.ApplicationServer.Http.Channels の 222 行目。F:\codeplex\wcf\Http\Src\Microsoft.ApplicationServer.Http\Microsoft\ApplicationServer\Http\Channels\HttpMessageEncodingRequestContext.cs:System.ServiceModel.Dispatcher.ChannelHandler.EnsureChannelAndEndpoint(RequestContext request) の 54 行目の HttpMessageEncodingRequestContext.get_RequestMessage() ) System.ServiceModel.Dispatcher.ChannelHandler.TryRetrifyingInstanceContext (RequestContext 要求) で
なぜこれが起こるのか分かりますか?