Web api get works, but post and delete don't work after posting in iis

I struggled for 4 hours and I still have not received any solution. I already apply some changes, but still my message and removing the api returns a 500 error.

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 works 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 use this web server in my configuration

<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>
+4
source share
2 answers

I had a similar problem and this is due to the name of the function that catches the request. For some reason, the GET functions did this automatically, but POST did not always do this. You could try to explicitly call it that way 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
}

, [System.Web.Http.HttpPost] [System.Web.Http.HttpPost, ActionName("SavePatient")]

0

"savepatient" Post. /api/ {controller} . HTTP . "SavePatient" "Post" ( "Put", ).

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
}

URL:

url: API_URL + "api/claim/"
0

Source: https://habr.com/ru/post/1685219/


All Articles