I've been struggling for 4 hours and I still didn't get any solution. I already apply some modification but still my post and delete api returns the error 500.
GET js
$.getJSON(API_URL + 'api/claim/search', params).done(function (data) { myJsonObject = data; d.resolve(data); }); return d.promise();
API
[Route("api/claim/search")] [System.Web.Http.AcceptVerbs("GET")] [System.Web.Http.HttpGet] public IEnumerable<ClaimInfo> Get([FromUri] ClaimSearch obj_ClaimSearch) { //my code }
This get method is working 100%
POST js
$.ajax({ type: "POST", data: JSON.stringify(p[0]), url: API_URL + "api/claim/" + (editorPage === "resubmission" ? "saveresubmissionpatient": "savepatient"), contentType: "application/json", success: function (data) { }, error: function () { } });
API
[Route("api/claim/savepatient")] [System.Web.Http.AcceptVerbs("POST")] [System.Web.Http.HttpPost] public Guid SavePatient([FromBody]ClaimInfo claimInfo) { //my code }
And here is my WebApi.Config.cs
public static void Register(HttpConfiguration config) { config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); }
I already apply this webserver thing in my config
<modules> <remove name="WebDAVModule" /> <add type="DevExpress.Web.ASPxHttpHandlerModule, DevExpress.Web.v16.2, Version=16.2.5.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" name="ASPxHttpHandlerModule" /> </modules> <handlers> <remove name="WebDAV" /> <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" /> <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" /> <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /> <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /> <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> </handlers>
2 Answers
Answers 1
I had a similar issue, and it had to do with the name of the function catching the request. For some reason, GET functions did this automatically but POST did not always do so. You could try explicitly naming it like this, and see if it also solves your problem:
[Route("api/claim/savepatient")] [System.Web.Http.AcceptVerbs("POST")] [System.Web.Http.HttpPost, ActionName("SavePatient")] public Guid SavePatient([FromBody]ClaimInfo claimInfo) { //my code }
Note that I changed [System.Web.Http.HttpPost]
to [System.Web.Http.HttpPost, ActionName("SavePatient")]
Answers 2
You have named the action "savepatient" instead of Post. The router matches /api/{controller} to your {controller}Controller Class. The HTTP method must match the public method of the class. Try renaming "SavePatient" to "Post" (or "Put" if you use that method).
public class ClaimController : ApiBaseController { //[Route("api/claim/")] don't need this public Guid Post([FromBody]ClaimInfo claimInfo) { //my code to add new claiminfo } //[Route("api/claim/")] don't need this public Guid Put([FromBody]ClaimInfo claimInfo) { //my code to edit claiminfo }
And remove the extra path on the url:
url: API_URL + "api/claim/"
0 comments:
Post a Comment