2016-11-17 21 views
0

GETメソッドのみが常に動作していますが、PUT、POST、およびDELETEでは常にエラーが発生します。 web.configとIISサイトの両方でハンドラーマッピングを更新しようとしました。 最初は、状態コード405でメソッドが許可されていないとしてエラーが発生しました。 415、のreasonPhrase:「サポートされていないメディアタイプIはPUT、POST、およびDelete RestAPIのエラー

としてハンドラマッピングを変更すると、 "サポートされていないメディアタイプ" .Followingは私が

{のStatusCodeを取得していますrepsonceあるよう

<system.webServer> 
    <handlers> 
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> 
    <remove name="OPTIONSVerbHandler" /> 
    <remove name="TRACEVerbHandler" /> 
    <remove name="WebDAV" /> 
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> 
    <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> 

    <remove name="ExtensionlessUrl-Integrated-4.0" /> 
    <add name="ExtensionlessUrl-Integrated-4.0" 
     path="*." 
     verb="GET,HEAD,POST,DEBUG,DELETE,PUT" 
     type="System.Web.Handlers.TransferRequestHandler" 
     preCondition="integratedMode,runtimeVersionv4.0" /> 
</handlers> 

<validation validateIntegratedModeConfiguration="false" /> 

<modules> 
    <remove name="ApplicationInsightsWebTracking" /> 
    <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" /> 
    <remove name="WebDAVModule"/> 
</modules> 

は415のためのエラーを取得し始めました'、バージョン:1.1、内容:System.Net.Http.StreamContent、ヘッダー: { キャッシュコントロール:no-cache プラグマ:no-cache サーバー:Microsoft-IIS/8.5 X-AspNet-Version:4.0 .30319 X-Powered-B y:ASP.NET 日付:2014年11月17日16:44:52 GMT コンテンツタイプ:application/octet-stream; charset = utf-8 有効期限:-1 コンテンツの長さ:100 }}

以下は私のAPIコールです

// PUT: api/CreditRequests/5 
    [ResponseType(typeof(void))] 
    public IHttpActionResult PutCreditRequest(Guid id, CreditRequest creditRequest) 
    { 
     if (!ModelState.IsValid) 
     { 
      return BadRequest(ModelState); 
     } 

     if (id != creditRequest.CreditRequestId) 
     { 
      return BadRequest(); 
     } 

     db.Entry(creditRequest).State = EntityState.Modified; 

     try 
     { 
      db.SaveChanges(); 
     } 
     catch (DbUpdateConcurrencyException) 
     { 
      if (!CreditRequestExists(id)) 
      { 
       return NotFound(); 
      } 
      else 
      { 
       throw; 
      } 
     } 

     return StatusCode(HttpStatusCode.NoContent); 
    } 

    // POST: api/CreditRequests 
    [ResponseType(typeof(CreditRequest))] 
    public IHttpActionResult PostCreditRequest(CreditRequest creditRequest) 
    { 
     if (!ModelState.IsValid) 
     { 
      return BadRequest(ModelState); 
     } 

     db.CreditRequests.Add(creditRequest); 

     try 
     { 
      db.SaveChanges(); 
     } 
     catch (DbUpdateException) 
     { 
      if (CreditRequestExists(creditRequest.CreditRequestId)) 
      { 
       return Conflict(); 
      } 
      else 
      { 
       throw; 
      } 
     } 

     return CreatedAtRoute("DefaultApi", new { id = creditRequest.CreditRequestId }, creditRequest); 
    } 

    // DELETE: api/CreditRequests/5 
    [ResponseType(typeof(CreditRequest))] 
    public IHttpActionResult DeleteCreditRequest(Guid id) 
    { 
     CreditRequest creditRequest = db.CreditRequests.Find(id); 
     if (creditRequest == null) 
     { 
      return NotFound(); 
     } 

     db.CreditRequests.Remove(creditRequest); 
     db.SaveChanges(); 

     return Ok(creditRequest); 
    } 

そして私はそれらをHttpClientオブジェクトを使って呼び出しています。コードは

string jsondata = JsonConvert.SerializeObject(item); 
      var content = new StringContent(jsondata, System.Text.Encoding.UTF8, "application/json"); 
      HttpResponseMessage response = null; 
      using (var client = GetFormattedHttpClient())// Adding basic authentication in HttpClientObject before using it. 
      { 
       if (IsNew == true) 
        response = client.PostAsync (_webUri, content).Result; 
       else if (IsNew == false) 
        response = client.PutAsync(_webUri, content).Result; 

      } 
      if (!response.IsSuccessStatusCode) 
           return false; 
      else 
      return true; 
+0

どこでこの使用法が見られましたか? '[ResponseType(typeof(void))] –

+0

DBにレコードを追加するPOSTメソッドをテストしています。 PUTの場合、NoContentとしてStatusCodeを取得することに集中しています –

+0

その属性を削除します。それは必要ではありません。 – Amy

答えて

0

となります。

protected async Task<String> connect(String URL,WSMethod method,StringContent body) 
{ 
     try 
     { 

      HttpClient client = new HttpClient 
      { 
       Timeout = TimeSpan.FromSeconds(Variables.SERVERWAITINGTIME) 
      }; 
      if (await controller.getIsInternetAccessAvailable()) 
      { 
       if (Variables.CURRENTUSER != null) 
       { 
        var authData = String.Format("{0}:{1}:{2}", Variables.CURRENTUSER.Login, Variables.CURRENTUSER.Mdp, Variables.TOKEN); 
        var authHeaderValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(authData)); 

        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeaderValue); 
       } 

       HttpResponseMessage response = null; 
       if (method == WSMethod.PUT) 
        response = await client.PutAsync(URL, body); 
       else if (method == WSMethod.POST) 
        response = await client.PostAsync(URL, body); 
       else 
        response = await client.GetAsync(URL); 

       .... 
} 
関連する問題