When do I need to use the POST method with multiple parameters instead of a separate model?

Could you tell me when I need to use several parameters instead of a separate model in my WebApi / MVC application?

I have an action that takes several parameters.

[HttpPost]
public InfoViewModel GetInfo(IEnumerable<Guid> Ids, DocumentType type)
{
    // to do smth
}

I can also convert this action to the following:

[HttpPost]
public InfoViewModel GetInfo(RequestViewModel model)
{
   // to do smth
}

I need a special model for the second case.

public class RequestViewModel
{
    public IEnumerable<Guid> Ids { get; set; }
    public DocumentType DocumentType { get; set; }
}

I send my data to the server in JSON format. Could you tell me about some of the advantages and disadvantages of these two approaches? Thank.

+4
source share
1 answer

The ASP.NET Web API and MVC will try to bind the parameters for the action method, as shown below.

  • (, ):
    • -API. . , .. -API JSON , MediaTypeFormatter ( Serializer). , , . ( ) (, [FromUri], [ModelBinder]), .
    • MVC. , .. , /. , ModelBinder MediaTypeFormatter, -API.
  • (, , ..):
    • -. /, .. URI . , [FromBody] .
    • MVC. , .. , /. , ModelBinder.

,

  • - - (, POST , DocumentType - , ). 2 , 1, , .
  • MVC. , 2 .

:

+3

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


All Articles