2016-12-12 5 views
0

うまくいけば簡単な質問ですが、自分でホストされている(これはうまくいきます)単純なアプリケーションを持っています。 (メソッドにヒットしますが、パラメータはnullにデフォルト設定されています)、POST要求のBODYの名前付きパラメータを未処理のJSON形式で渡すと、400応答が返され、何らかの理由でそのメソッドにヒットしません。自己ホストREST Webサービスでパラメータ付きのPOSTを受け付けない

アドバイスありがとうございます。

[環境:Visual Studioの2015、C#の、自己ホスト型RESTアプリケーション]

[コード詳細]

(自己のためにコードをホスティングしているWebサービスがアプリをホスト)

WebServiceHost host = new WebServiceHost(typeof(CalculatorServiceREST), new Uri("http://localhost:8000")); 
ServiceEndpoint ep = host.AddServiceEndpoint(typeof(ICalculator), new WebHttpBinding(), ""); 
ServiceDebugBehavior stp = host.Description.Behaviors.Find<ServiceDebugBehavior>(); 
stp.HttpHelpPageEnabled = false; 
host.Open(); 
Console.WriteLine("Service is up and running"); 
Console.WriteLine("Press enter to quit "); 
Console.ReadLine(); 
host.Close(); 

(契約実装クラス:CalculatorServiceRest.cs)

public class CalculatorServiceREST : ICalculator 
{ 
    [WebInvoke(Method = "POST")] // POST to: /RoundUp (with BODY item of 'number' in the request) 
    public int RoundUp(double number) 
    { 
     return Convert.ToInt32(Math.Round(number)); 
    } 

    [HttpPost] // POST to: /GetName (with BODY item of 'number' in the request) 
    public string GetName(string name) 
    { 
     return name; 
    } 
} 
+1

可能な回答については、この質問を確認してください。https://stackoverflow.com/q/12899360 –

答えて

0

私はPOSTメソッドに次の属性値を追加して、JSONデータをBODYに渡すことができるようにしなければなりません(これはMVCの[HttpPost]属性を置くだけでなく、それはそれを拾う方法を知っているだけです。それはなぜ異なっているかについて誰かがいくつかの洞察を提供することができますか?

[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] 
関連する問題