ASP.NET WebApi: (405) Method not allowed

I have a web api controller. It behaves very weird. When I use PostMan, I can access the POST method in Web Api, but when I use the HttpWebRequest from .net, it returns (405) The method is not allowed. I put the web api code here:

using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web; using System.Web.Http; namespace MyProject.Controllers { public class TestController : ApiController { [HttpPost] public int buy(OrderResult value) { try { if (value.InvoiceDate != null && value.Result == 1) { return 0; } } catch{} return -1; } } public class OrderResult { public int Result { get; set; } public long InvoiceNumber { get; set; } public string InvoiceDate { get; set; } public long TimeStamp { get; set; } } } 

Here is my WebApiConfig.cs:

 using System; using System.Collections.Generic; using System.Linq; using System.Web.Http; namespace MyProject { public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { action = RouteParameter.Optional, id = RouteParameter.Optional } ); } } } 

This is how I send a POST request from another .NET project:

 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Web; namespace MyProject.Controllers { public static class WebReq { public static string PostRequest(string Url, string postParameters) { try { HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(Url); myReq.Method = "POST"; myReq.Timeout = 30000; myReq.Proxy = null; byte[] postData = Encoding.UTF8.GetBytes(postParameters); myReq.ContentLength = postData.Length; myReq.ContentType = "application/x-www-form-urlencoded"; using (Stream requestWrite = myReq.GetRequestStream()) { requestWrite.Write(postData, 0, postData.Length); requestWrite.Close(); using (HttpWebResponse webResponse = (HttpWebResponse)myReq.GetResponse()) { if (webResponse.StatusCode == HttpStatusCode.OK) { using (Stream str = webResponse.GetResponseStream()) { using (StreamReader sr = new StreamReader(str)) { return sr.ReadToEnd(); } } } return null; } } } catch (Exception e) { var message = e.Message; return null; } } } } 

I have already added the following code to my web.config:

 <modules runAllManagedModulesForAllRequests="true"> <remove name="WebDAVModule" /> </modules> 

This is strange because I can send a POST request from PostMan. PostMan sends this code to the API.

 POST /api/Test/buy HTTP/1.1 Host: domain.com Cache-Control: no-cache Postman-Token: 9220a4e6-e707-5c5f-ea61-55171a5dd95f Content-Type: application/x-www-form-urlencoded InvoiceDate=28012016&Result=1 

I will be grateful for any suggestions to solve the problem.

+6
source share
5 answers

I have found a solution.

I checked the Fiddler request. When I send a POST request to the API, it is automatically redirected to the same address with this new parameter AspxAutoDetectCookieSupport=1

How to remove AspxAutoDetectCookieSupport = 1

Finally, I changed cookieless="AutoDetect" in web.config to cookieless="UseCookies" and the problem was resolved.

+2
source

I know this is an old post, but maybe this can help someone else. I just had a similar problem: I got the 405 "Not Allowed" error message when I explicitly made a post in the postman. It turned out I was sending to the URL using http instead of https. A change in https fixed this.

+3
source
  byte[] postData = Encoding.UTF8.GetBytes(postParameters); myReq.ContentLength = postData.Length; myReq.ContentType = "application/x-www-form-urlencoded"; using (Stream requestWrite = myReq.GetRequestStream()) { requestWrite.Write(postData, 0, postData.Length); 

Most likely you are not sending the correct x-www-form-urlencoded request. You are probably not encoding data properly from what is passed as postParameters. See Publish form data using HttpWebRequest

Since you are not creating a valid OrderResult object according to x-www-form-urlencoded, the route selector does not select your Buy action. That is why you are not getting POST.

You will probably get to the controller if you change OrderResult value = null , since now it is an optional parameter. However, this is not what you want unless you have a weird controller that behaves like this:

 Buy(OrderResult value = null) { if(value== null) value = MyCustomDeserializeFromRequestBody(RequestContext) ... } 

Ultimately, you simply should not use this class, there are much better designs for modern development fooobar.com/questions/256756 / ... https://github.com/hhariri/EasyHttp from the top of the head

+2
source

In my case, I had a physical folder inside the project with the same name as the route (for example, a sandbox), and any POST request was intercepted by the static file handler in IIS (obviously) instead of the WebAPI runtime.

Getting the misleading 405 error instead of the more expected 404 was the reason I took some time to troubleshoot.

It is not easy to fall into it, but it is possible. Hope this helps someone.

0
source

Catch your request with a violinist or something similar, you are probably sending the "OPTIONS" method.

This is related to CORS, so I think there will be a solution for you to enable Enable Cors in WebAPI

http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

-1
source

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


All Articles