There is the following code

public string Post(string value) { return value; } 

Why does such code in the MVC 5 controller work fine, and in the Web Api 2 controller I get a 405 Method Not Allowed error with the following HttpBody {"Message":"Запрошенный ресурс не поддерживает HTTP-метод \"POST\"."} ? What exactly and why causes such different behavior?

UPD

I do not ask you to tell me how to do it so that it works - I know: we must either accept not the string , but the DTO, or put [FromBody] in front of the parameter and send =value in the request body. I'm interested in the question above

  • this is a server issue: 405 method not allowed web api - Grundy
  • And what request do you send GET or POST? For WebApi, methods with the names Get, Post, Delete (and seemingly Put) are considered to be marked by default with the corresponding attribute (HttpGetAttribute, HttpPostAttribute, etc.) - Vadim Sentiaev
  • @VadimSentyaev, yes, Post, of course - Qutrix
  • If you ported code from MVC to WebApi, then check this answer at stackoverflow.com/questions/15718741/… - Vadim Sentiaev
  • @VadimSentyaev, no, that's not the reason - Qutrix

2 answers 2

By default, the Web API uses the following rules to bind parameters:

If the parameter is a “simple” type, the Web API tries to get the value from the URI. Simple types include: (More about type converters later.) For complex types, it makes it possible to use a media-type formatter.

https://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

  • one
    Please try to publish detailed answers containing a specific example of the minimum solution in Russian , supplementing them with a link to the source. Answers –references (like comments) do not add knowledge to Runet. - Nicolas Chabanovsky
  • This is only half the answer, I am mostly interested in the second half of my question: why did the developers decide to make different behaviors for the two related technologies? The reason must be the same - Qutrix
  • one
    Definitely difficult to answer. Most likely to speed up the parsing model. For ApiController made their implementation of IActionValueBinder. github.com/WebApiContrib/WebAPIContrib is an implementation of IActionVinder in the style of mvc - zayats
  • one
    WebApi was made later by MVC, so they made a separate implementation. In ASP.NET Core, they made just one implementation for MVC and WebApi. The answer is very simple - it happened so historically, plus they didn’t want to break backward compatibility. - Vadim Sentiaev
  • @zayats, interesting githabchik, as I understand it, is supported by enthusiasts? And why did you decide to speed up web api, and didn't speed up mvc?) - Qutrix

Problem with request headers. Either return json or change the title to text / plain

  • firstly, it does not decide secondly, what does "return json" mean? thirdly, "change the title to text / plain" - it so happens that not always I (always not me) establish the rules of the game (titles) - Qutrix
  • "it happens that not always I (always not me) set the rules of the game" - in fact, because you develop api, then you dictate how clients should work with it, and not vice versa. - Vadim Sentiaev
  • one
    Try to write more detailed answers. Explain what is the basis of your statement? - Nicolas Chabanovsky
  • @VadimSentyaev, but I want the customers to be comfortable and they can choose the way that is simpler / better / more acceptable for them, is this really bad? It seems to me that clients set the rules of the game, because if I don’t do it, someone else will do it - Qutrix
  • @Qutrix just do api by standards. Users like this. - Vadim Sentiaev