ASP.NET Web Api: The requested resource does not support http method 'GET'
If you have not configured any HttpMethod on your action in controller, it is assumed to be only HttpPost in RC. In Beta, it is assumed to support all methods - GET, PUT, POST and Delete. This is a small change from beta to RC. You could easily decore more than one httpmethod on your action with [AcceptVerbs("GET", "POST")].
The requested resource does not support HTTP method 'GET'
Please use the attributes from the System.Web.Http namespace on your WebAPI actions:
[System.Web.Http.AcceptVerbs("GET", "POST")]
[System.Web.Http.HttpGet]
public string Auth(string username, string password)
{...}
The reason why it doesn't work is because you were using the attributes that are from the MVC namespace System.Web.Mvc
. The classes in the System.Web.Http
namespace are for WebAPI.
How to fix - The requested resource does not support http method 'POST'
You have declared route which requires url parameters
[Route("rename/{userId}/{type}/{title}/")]
So when you send request to api/customer/rename
it does not match this method. You should remove parameters which you are passing in request body from route parameters
[Route("rename")]
Make sure that you have appropriate RoutePrefix("api/customer")
attribute on your controller.
Second problem is multiple [FromBody]
parameters. You will get can't bind multiple parameters error. There is limitation - you can mark only one parameter as FromBody. See Sending Simple Types notes:
Web API reads the request body at most once, so only one parameter of
an action can come from the request body. If you need to get multiple
values from the request body, define a complex type.
You should create complex type which will hold all parameters
public class RenameModel
{
public int UserId { get; set; }
public string Type { get; set; }
public string Title { get; set; }
}
And change method signature to
[HttpPost]
[Route("rename")]
public IHttpActionResult Rename(RenameModel model)
And send request data as application/x-www-form-urlencoded
The requested resource does not support http method 'OPTIONS' when using EnableCors
You've probably missed the higher level call to HttpConfiguration.EnableCors
, as described here: https://enable-cors.org/server_aspnet.html.
Add this code to your configuration:
public static void Register(HttpConfiguration config)
{
// New code
config.EnableCors();
}
ASP.NET Web API - The requested resource does not support http method 'GET'
You don't need to create a route table by HttpMethods because you have [HttpPost]
attribute.
Replace all config.Routes.MapHttpRoute
by
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
It's preferable to use Attribute Routing for Web API project. It will reduce the chances of errors because in RouteConfig class there can be any mistake while creating a new custom route. Also, you don’t have to take care of the routing flow i.e, from most specific to most general. All here is the attribute you use the action method.
ASP.NET Web Api The requested resource does not support http method PUT/DELETE
You might still have some extra modules installed. Even if you get those fixed, your request and routes won't match.
Your route config is being ignored as you have explicitly set the routes using your route attributes. You should update your DELETE method route attribute to [Route("{id:int}")]
. For the PUT method, it is a little more unclear as to what you are doing, but I would assume you would want to do something like this:[Route("{id:int}")]
[HttpPut]
public HttpResponseMessage Put([FromUrl]int id, [FromBody]SupplyItem item) {
...
The requested resource does not support http method 'GET' appears after an inner exception
Look at the following post: Exception Handling in ASP.NET Web API
It is necessary take care to handle any exceptions raised in Web API methods depend on your method functionality.
In example above instead of throw ex;
it is necessary to construct and return some instance of HttpResponseException()
too.
Related Topics
How to Add Code Outside the Scope of Main When Using C# 9 Top Level Statements
Use Different Name for Serializing and Deserializing with JSON.Net
Nullable Object Must Have a Value
What Is C# Analog of C++ Std::Pair
Execute Unit Tests Serially (Rather Than in Parallel)
Print Rdlc Report Without Showing Reportviewer Control
Newtonsoft Inline Formatting for Subelement While Serializing
What Task Is Best Done in a Functional Programming Style
Overriding Fields or Properties in Subclasses
Returning a String from a C# Dll with Unmanaged Exports to Inno Setup Script
Copying Free Hand Drawing from Panel in Visual Studio 2013
Open a Folder Using Process.Start
C# Constructing Parameter Query SQL - Like %
ASP.NET Identity Change Password
Binding to Custom Control Inside Datatemplate for Itemscontrol
Calculating Hmacsha256 Using C# to Match Payment Provider Example