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

為{幸葍}努か 提交于 2019-12-23 11:58:06

问题


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

I have an action which takes a few parameters.

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

Also I can transform 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 advantages and disadvantages of these two approaches? Thanks.


回答1:


ASP.NET Web API and MVC will try to bind parameters for an action method as below.

  1. Complex Type Parameters (e.g. Custom class):
    • Web API - Binds from the Request Body by default. But this is limited to only one parameter i.e. Web API will bind the Request Body JSON to only one complex type parameter using a MediaTypeFormatter (in simple terms Serializer). It's because the Request Body is stored in a non-buffered stream by default which can be read once. Remaining complex type parameters (if any) can only be bind using any of the binding attributes (e.g. [FromUri], [ModelBinder]) by applying them to the parameter.
    • MVC - Binds from everywhere i.e. Request Body, Route/Query String data. This is because it uses ModelBinder by default compared to MediaTypeFormatter used by Web API.
  2. Simple Type Parameters (e.g. Primitive Types, string etc):
    • Web API - Bind either from Route/Query String data i.e. from URI by default. But they can be forced to bind from body by applying [FromBody] attribute or some Type Converter to the parameter.
    • MVC - Binds from everywhere i.e. Request Body, Route/Query String data. This is because it uses ModelBinder by default.

Back to your question,

  1. Web API - (Assuming your POST request will send the data in the Request Body and DocumentType is a class and no binding attribute is applied to it) Only the Approach 2 bind the model properly but not Approach 1 as both are complex type parameters and no binding attribute is applied to any one of them.
  2. MVC - Both works fine but Approach 2 is much better for readability and extensibility to add any additional properties in the future.

More info:

  • https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api
  • https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding
  • https://blogs.msdn.microsoft.com/jmstall/2012/04/16/how-webapi-does-parameter-binding/


来源:https://stackoverflow.com/questions/42529639/when-i-need-to-use-post-method-with-multiple-parameters-instead-of-separate-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!