ASP.NET WebApi : (405) Method Not Allowed

405 method not allowed web api put c#
405 method not allowed web api .net core
asp.net core 405 method not allowed
405 method not allowed web api get c#
405 method not allowed iis
how to fix 405 method not allowed
dotnet core 405 method not allowed
405 method not allowed post

I have a Web Api controller. It behaves very strange. When I use PostMan I can access the POST method on the Web Api but when I use HttpWebRequest from .net it returns (405) Method 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 the 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 already added the following code in my web.config:

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

It is strange because I can send POST request successfully from PostMan. The PostMan send 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 appreciate any suggestion to solve the problem.

I know this is an old post, but maybe this might help someone else. I just had a similar issue, getting a 405 error "Get not allowed" when I was clearly doing a post in postman. Turned out, I was submitting to the URL using http instead of https. Changing to https fixed it.

Troubleshoot Web API2 apps that work in Visual Studio and fail on a , You are POSTing from the client: await client.PostAsJsonAsync("api/products", product);. not PUTing. Your Web API method accepts only PUT  ASP.NET Core Web API PUT and DELETE Methods not allowed (405 Error) When Deployed to Live Environment. Apr 11, 2016 ASP.NET, Domains & Hosting, Web Design.. Ran into this issue after deploying our latest web app built on ASP.NET 5 (now known as Core 1.0), MVC 6 with Web API.

I found the solution.

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

How to remove AspxAutoDetectCookieSupport=1

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

405 method not allowed Web API, Moriyama. Moriyama build, support and deploy Umbraco, Azure and ASP.NET websites and applications. Find out  So, I am stuck in a strange behavior, that is, I am able to send(or POST) data using Postman (plugin of chrome) or using RESTClient(extension of Firefox), . but not able to send it from my html file which lies outside the project.

     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);

You're most likely not sending a proper x-www-form-urlencoded request. You're likely not encoding the data properly from whatever is passed in as postParameters. See Post form data using HttpWebRequest

Since you're not generating a valid OrderResult object according to x-www-form-urlencoded, the route selector does not select your Buy action. This is why you get POST is not allowed.

You would likely get to the controller if you changed OrderResult value = null as it is now an optional parameter. However this isn't what you want, unless you're going to have a weird controller that behaves like:

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

    ...
}

Ultimately you just really shouldn't use this class there are much better constructs for modern development https://stackoverflow.com/a/31147762/37055 https://github.com/hhariri/EasyHttp off the top of my head

How to solve the Web API error 405 Method Not Allowed , 100. Hi Norbert,. Take a look at these two threads: http://stackoverflow.com/​questions/35065385/asp-net-webapi-405-method-not-allowed. If HttpPost/HttpDelete attributes won't precisely reflect the Web API endpoint, HTTP status 405 Method not allowed is returned to the client. I believe this status message to be inappropriate, confusing and misleading. Describe the solution you'd like. I suggest to, instead, return one of the following status codes in the above case: 400 Bad

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

Getting a misleading 405 error instead of the more expected 404, was the reason it took me some time to troubleshoot.

Not easy to fall-into this, but is possible. Hope it helps someone.

httpclient post to web api method returns 405 method not allowed , The 405 Method Not Allowed is an HTTP response status code indicating that the specified request HTTP method was received and  I have a Web Api controller. It behaves very strange. When I use PostMan I can access the POST method on the Web Api but when I use HttpWebRequest from .net it returns (405) Method Not Allowed. I p

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

That's related to CORS, so I think solution for you will be to Enable Cors on WebAPI

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

405 Method Not Allowed: What It Is and How to Fix It, iI have a problem with webapi 2 on the production: With the Web API Put Request generates an Http 405 Method Not Allowed error:. I really need help I've set up WebApi within Umbraco and for now I am not able to send POST requests to my IIS anymore over HttpClient. GET works ok. Here on stackoverflow is a detailed explaination sorry for not copying here again.

WEB API 405 ON POST Method, with a status of 405 (method not allowed)" error In Web API. I was working with Web API for one of my new projects, and suddenly I got this error “the better off posting it on C# Corner, Code Project, Stack Overflow, ASP. Summary HTTP 405 errors are caused when an HTTP method is not allowed by a web server for a requested URL. This condition is often seen when a particular handler has been defined for a specific verb, and that handler is overriding the handler that you expect to process the request.

The Server Responded with a Status of 405 (Method Not Allowed), I`m working on a WebAPI application (ASP.NET MVC, Entity Framework) & trying to DELETE an entry from Database. Below is the code related  Apart from when he's with his wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it. Related posts How to flip an image using CSS

WebAPI - [DELETE] 405 Method Not Allowed, 404 Not Found; 501 Not Implemented. Additional context. See the following two StackOverflow issues: ASP.NET Core Web API / Kestrel  Despite the rather easy workaround, such an issue is definitely a though one, as it will easily affect most ASP.NET Core Web API and Web Applications when they get deployed on a live environment: that’s because the WebDAV module, although not supported by IIS Express, happens to be enabled in most production servers.

Comments
  • Great man, you saved my day!!
  • It's not a CORS issue, that was actually my first thought but OP is running purely from a server/application not in a browser+ajax.