Problem: when accessing an Api-Delete interface from WPF on Asp.Net Core MVC, the response StatusCode 405 Method not allowed is returned.
Call from the client side (wpf)
string sURL_Api_Delete = app_settings._Api_Server + "/api/projects/" + sID_on_Server; HttpResponseMessage httpResponseMessage = null; try { httpResponseMessage = await client.DeleteAsync(sURL_Api_Delete); clsSys.fx_Log("ok.Deleted " + sID_on_Server); } catch (Exception ex) { clsSys.fx_Error_Log("Error Delete on Server: : " + ex.Message + "IDProject_on_Server=" + sID_on_Server); } //</ read webApi >
|
With the URL
httpResponseMessage = await client.DeleteAsync("https://freiberufler-jobs.de/api/projects/10072"); |
On the server side
Asp.Net Core Api
//HttpClient-URL: httpDelete /api/projects/5 [HttpDelete("{id}")] public async Task< ActionResult> Delete(int id) { //-------------< HttpDelete(ID) >------------- //< get UserClaim Info > //*get User from Token var userClaim_in_Token = HttpContext.User.Claims.Where(c => c.Type == ClaimsIdentity.DefaultNameClaimType).FirstOrDefault(); //User as Name if (userClaim_in_Token == null) { return null; }
|
Reason:
The return value is completely correct.
The StatusCode 405 and "Method not allowed" comes from the Asp.Net MVC action response BadRequest ().
In this case, it was checked whether the record also belongs to the client.
If both are not equal then the answer: "Method not allowed" is returned
//< check Owner > long IDOwner = project.IDOwner; if (IDOwner != IDCurrent_User) { return BadRequest(); } //</ check Owner > |