2016-11-30 3 views
1

이 요청Put 요청은 BadRequest를 반환합니다. , 가져 오기 삭제하고 포스트가 오류없이 작동

  var customStore = new DevExpress.data.CustomStore({ 
 
    load: function(loadOptions) { 
 
     return $.getJSON('/erg/api/api/Caveats'); 
 
    }, 
 
    byKey: function(key) { 
 
     return $.getJSON('http://webcrm/erg/api/api/Caveats' + "/" + encodeURIComponent(key)); 
 
    }, 
 
    insert: function(values) { 
 
     return $.post('http://webcrm/erg/api/api/Caveats', values); 
 
    }, 
 
    update: function(key, values) { 
 
     return $.ajax({ 
 
      url: 'http://webcrm/erg/api/api/Caveats' + "/" + encodeURIComponent(key), 
 
      method: "PUT", 
 
\t \t \t data: values 
 
     }); 
 
    }, 
 
    remove: function(key) { 
 
     return $.ajax({ 
 
      url: 'http://webcrm/erg/api/api/Caveats' + "/" + encodeURIComponent(key), 
 
      method: "DELETE", 
 
     }); 
 
    }, 
 
    key: "CaveatID" 
 
});
여기

을 만드는 페이지는 컨트롤러

using System; 
 
using System.Collections.Generic; 
 
using System.Data; 
 
using System.Data.Entity; 
 
using System.Data.Entity.Infrastructure; 
 
using System.Linq; 
 
using System.Net; 
 
using System.Net.Http; 
 
using System.Web.Http; 
 
using System.Web.Http.Description; 
 
using CRMApi.Models; 
 

 
namespace CRMApi.Controllers 
 
{ 
 
    public class CaveatsController : ApiController 
 
    { 
 
     private CaveatEntities db = new CaveatEntities(); 
 

 
     // GET: api/Caveats 
 
     public IQueryable<Caveat> GetCaveats() 
 
     { 
 
      return db.Caveats; 
 
     } 
 

 
     // GET: api/Caveats/5 
 
     [ResponseType(typeof(Caveat))] 
 
     public IHttpActionResult GetCaveat(int id) 
 
     { 
 
      Caveat caveat = db.Caveats.Find(id); 
 
      if (caveat == null) 
 
      { 
 
       return NotFound(); 
 
      } 
 

 
      return Ok(caveat); 
 
     } 
 

 
     // PUT: api/Caveats/5 
 
     [ResponseType(typeof(void))] 
 
     public IHttpActionResult PutCaveat(int id, Caveat caveat) 
 
     { 
 
      if (!ModelState.IsValid) 
 
      { 
 
       return BadRequest(ModelState); 
 
      } 
 

 
      if (id != caveat.CaveatID) 
 
      { 
 
       return BadRequest(); 
 
      } 
 

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

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

 
      return StatusCode(HttpStatusCode.NoContent); 
 
     } 
 

 
     // POST: api/Caveats 
 
     [ResponseType(typeof(Caveat))] 
 
     public IHttpActionResult PostCaveat(Caveat caveat) 
 
     { 
 
      if (!ModelState.IsValid) 
 
      { 
 
       return BadRequest(ModelState); 
 
      } 
 

 
      db.Caveats.Add(caveat); 
 
      db.SaveChanges(); 
 

 
      return CreatedAtRoute("DefaultApi", new { id = caveat.CaveatID }, caveat); 
 
     } 
 

 
     // DELETE: api/Caveats/5 
 
     [ResponseType(typeof(Caveat))] 
 
     public IHttpActionResult DeleteCaveat(int id) 
 
     { 
 
      Caveat caveat = db.Caveats.Find(id); 
 
      if (caveat == null) 
 
      { 
 
       return NotFound(); 
 
      } 
 

 
      db.Caveats.Remove(caveat); 
 
      db.SaveChanges(); 
 

 
      return Ok(caveat); 
 
     } 
 

 
     protected override void Dispose(bool disposing) 
 
     { 
 
      if (disposing) 
 
      { 
 
       db.Dispose(); 
 
      } 
 
      base.Dispose(disposing); 
 
     } 
 

 
     private bool CaveatExists(int id) 
 
     { 
 
      return db.Caveats.Count(e => e.CaveatID == id) > 0; 
 
     } 
 
    } 
 
}
입니다 put 요청이 실패하고 다시 실행됩니다. 400의 잘못된 요청 오류가 발생합니다. 다른 사람들은 정상적인 200 코드로 잘 작동합니다.

나는 바이올린을 사용하여 오류를 분석하는 등 가능한 모든 작업을 시도했습니다. broser에서 url을 열 수 있으며 아약스로 포맷 된 모든 데이터를 볼 수 있습니다.

은 webconfig 파일

에게 있습니다

The wrbapi was generated using asp.net and is connecting to a sql database 
 

 
    <?xml version="1.0" encoding="utf-8"?> 
 
<!-- 
 
    For more information on how to configure your ASP.NET application, please visit 
 
    http://go.microsoft.com/fwlink/?LinkId=301879 
 
    --> 
 
<configuration> 
 
    <configSections> 
 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
 
    </configSections> 
 
    <connectionStrings> 
 
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-CRMApi-20161123093835.mdf;Initial Catalog=aspnet-CRMApi-20161123093835;Integrated Security=True" providerName="System.Data.SqlClient" /> 
 
    <add name="CaveatEntities" connectionString="metadata=res://*/Models.Caveat.csdl|res://*/Models.Caveat.ssdl|res://*/Models.Caveat.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=192.168.16.228;initial catalog=ERGSERVER;persist security info=True;user id=sa;password=Red0ne?!123;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> 
 
    <add name="ERGSERVEREntities" connectionString="metadata=res://*/Controllers.SalesSupportExecutive.csdl|res://*/Controllers.SalesSupportExecutive.ssdl|res://*/Controllers.SalesSupportExecutive.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=192.168.16.228;initial catalog=ERGSERVER;persist security info=True;user id=sa;password=Red0ne?!123;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" /> 
 
    <add name="ERGSERVEREntities1" connectionString="metadata=res://*/Models.SalesSupportExecutiveModel.csdl|res://*/Models.SalesSupportExecutiveModel.ssdl|res://*/Models.SalesSupportExecutiveModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=192.168.16.228;initial catalog=ERGSERVER;persist security info=True;user id=sa;password=Red0ne?!123;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" /> 
 
    <add name="BacksModel" connectionString="data source=192.168.16.228;initial catalog=ERGSERVER;persist security info=True;user id=sa;password=Red0ne?!123;multipleactiveresultsets=True;application name=EntityFramework" providerName="System.Data.SqlClient" /></connectionStrings> 
 
    <appSettings></appSettings> 
 
    <system.web> 
 
    <authentication mode="None" /> 
 
    <compilation debug="true" targetFramework="4.5.2" /> 
 
    <httpRuntime targetFramework="4.5.2" /> 
 
    <httpModules> 
 
     <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" /> 
 
    </httpModules> 
 
    </system.web> 
 
    
 
    <system.webServer> 
 
    <modules> 
 
     <remove name="FormsAuthentication" /> 
 
     <remove name="ApplicationInsightsWebTracking" /> 
 
     <remove name="WebDAV" /> 
 
     <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" /> 
 
    </modules> 
 
    <handlers> 
 
     <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> 
 
     <remove name="WebDAVModule" /> 
 
     <remove name="OPTIONSVerbHandler" /> 
 
     <remove name="TRACEVerbHandler" /> 
 
     <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> 
 
    </handlers> 
 
    <validation validateIntegratedModeConfiguration="false" /> 
 
    </system.webServer> 
 
    <runtime> 
 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
 
     <dependentAssembly> 
 
     <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" /> 
 
     <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
 
     </dependentAssembly> 
 
     <dependentAssembly> 
 
     <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" /> 
 
     <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
 
     </dependentAssembly> 
 
     <dependentAssembly> 
 
     <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" /> 
 
     <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
 
     </dependentAssembly> 
 
     <dependentAssembly> 
 
     <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" /> 
 
     <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
 
     </dependentAssembly> 
 
     <dependentAssembly> 
 
     <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /> 
 
     <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> 
 
     </dependentAssembly> 
 
     <dependentAssembly> 
 
     <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" /> 
 
     <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" /> 
 
     </dependentAssembly> 
 
     <dependentAssembly> 
 
     <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" /> 
 
     <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> 
 
     </dependentAssembly> 
 
     <dependentAssembly> 
 
     <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> 
 
     <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
 
     </dependentAssembly> 
 
     <dependentAssembly> 
 
     <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
 
     <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" /> 
 
     </dependentAssembly> 
 
     <dependentAssembly> 
 
     <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> 
 
     <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
 
     </dependentAssembly> 
 
     <dependentAssembly> 
 
     <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" /> 
 
     <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" /> 
 
     </dependentAssembly> 
 
     <dependentAssembly> 
 
     <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" /> 
 
     <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" /> 
 
     </dependentAssembly> 
 
    </assemblyBinding> 
 
    </runtime> 
 
    <entityFramework> 
 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 
 
     <parameters> 
 
     <parameter value="mssqllocaldb" /> 
 
     </parameters> 
 
    </defaultConnectionFactory> 
 
    <providers> 
 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
 
    </providers> 
 
    </entityFramework> 
 
    <system.codedom> 
 
    <compilers> 
 
     <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" /> 
 
     <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" /> 
 
    </compilers> 
 
    </system.codedom> 
 
</configuration>

감사

답변

0

당신이 당신의 Asp.NET의 webapi 프로젝트에서 구성 항목을 다음과 같은 경우 확인하시기 바랍니다. PUT 및 DELETE를 허용하려면 WebDAv 및 WebDAVModule을 제거해야합니다.

<system.webServer> 
    <handlers> 
     <remove name="WebDAV" /> 
    </handlers> 
    <modules runAllManagedModulesForAllRequests="true"> 
     <remove name="WebDAVModule" /> 
    </modules> 
    </system.webServer> 

여전히 작동하지 않는 경우 IIS 관리자에서 처리기 매핑으로 이동하여 PUT 동사가 허용되는지 확인하십시오. ExtensionlessUrlHandler-Integrated-4.0을 찾아 두 번 클릭하여 엽니 다. 다음으로 요청 제한 버튼을 클릭하고 동사 탭을 확인한 다음 PUT이 없으면 추가하거나 모든 동사 허용을 선택하십시오.

업데이트 :

PUT을 위해 클라이언트 측 코드가 제안 내가 웹 설정 파일을 변경하고 또한 내가보고 한 extensionlessUrlHandler에와 처음 추가 된이

update: function(key, values) { 
     return $.ajax({ 
      url: 'http://webcrm/erg/api/api/Caveats/' + encodeURIComponent(key), 
      type: 'PUT', 
      contentType: 'application/json; charset=utf-8', 
      data: values 
     }); 
    } 
+0

감사합니다 비 노드에 호출 수정 PUT을 누른 다음 모든 동사를 선택했습니다. 하지만 기본 사이트에서이 작업을 수행했습니다. 그것은 여전히 ​​나에게 나쁜 요청 오류를 준다. 이유는 모르지만 제안에 감사드립니다. – user3487020

+0

webconfig 파일도 추가했습니다 – user3487020

+0

Delete 메소드가 작동합니까? PUT에 문제가 있습니까? – Vinod