WebApi2 The requested resource does not support mail

I read a ton of similar messages that describe the same error message, but they don't seem to match what I'm experiencing.

I recently started using the web API and ripped off all my MVC methods, from where I returned JSON, etc., so MVC will just display html and I will call the model via ajax from my webapi controllers.

Here's the weird thing, I can get and post from my Home apiController (so that I can login / register, etc.), but I can only get from the API controller in the area that I created. I get 405 (the method is not valid), although it is decorated and called in the same way as the other controller. I assume that routing is in order, otherwise it will not return my initial income?

Routing

public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultAreaApi", routeTemplate: "api/{area}/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); } 

controller

  // Returns Model [HttpGet] public HttpResponseMessage SelectAgent() // The requested resource does not support http method 'POST'. [HttpPost] public HttpResponseMessage SelectAgent(Guid id) 

JQuery

  // Works fine $.ajax({ type: 'POST', url: '/api/Home/Login', headers: options.headers, contentType: "application/json; charset=utf-8", dataType: 'JSON', data: ko.toJSON(self.serverModel), success: function (response) { // Works fine $.getJSON("/api/Account/Users/SelectAgent", function (model) { .... // 405 $.ajax({ type: 'POST', url: '/api/Account/Users/SelectAgent', headers: options.headers, contentType: "application/json; charset=utf-8", dataType: 'JSON', data: "{'id':'" + selectModel.agentId() + "'}", success: function (response) {.... 

the transmitted data seems fine (or at least for the MVC controller he worked with).

I have not modified the Home API Controller at all, I don’t understand how I can publish this, and not my other controller. Argh.

Any pointers in the right direction would be awesome.

+6
source share
3 answers

The Web API only scans query parameters for basic data types. So your post just looks at the URL /api/Account/Users/SelectAgent when you do your post. The presented data is not taken into account when comparing with the function, since you do not mark the Guid attribute [FromBody] . Thus, the error "Method not allowed" is returned, because it sends your POST request to your GET method (without parameters).

You can learn more about this on asp.net :

If the parameter is a β€œsimple” type, the Web API attempts to retrieve the value from the URI. Simple types include primitive .NET types (int, bool, double, etc.), as well as TimeSpan, DateTime, Guid, decimal and string, as well as any type with a type converter that can convert from a string. (More on type converters later.)

To fix this, try one of the following:

  • Change the url you submit to include id in the email request line

    url: '/api/Account/Users/SelectAgent?id=' + selectModel.agentId()

  • Change the action signature to read the FromBody identifier:

    public HttpResponseMessage SelectAgent([FromBody]Guid id)

+12
source

I know this is an old question, but this answer might help someone. Changing the signature of an action to read Id FromBody: didn't work for me, and I'm wondering how easy it is to pass parameters to the queue when you have a complex type with many fields / members.

This problem arises because both traditional and verb routing cannot be used in the same ApiController. See this link . You can solve this problem by adding an empty [Route] method to the POST action method. It: -

  [HttpPost] [Route] public HttpResponseMessage SelectAgent(Guid id) 
+3
source

When you have the same name for your message and the get function, it goes to the Get function. That is why you get this answer. The easiest way today is

  [HttpPost] [Route("api/postagent", Name ="postagent")] public HttpResponseMessage SelectAgent(Guid id) 

just define a route for your mail function, and on the client side place it using host + / api / postagent, for example. You will find more information at this link.

This is especially useful if you are creating an api controller with a new controller generator. Predefined functions will have a structure such as the get method and post will have the same route. you need to override one of them or go around both of them.

https://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#route-names

+1
source

All Articles