How to call a controller method that accepts two models?

It seems perfectly true if we have an API controller with a method like below:

[HttpPost] 
public IHttpActionResult DoStuff([FromBody]ModelA modelA, [FromBody]ModelB modelB)

Pay attention to two attributes [FromBody].

The question is how to call such a method?

localhost/test/DoStuff/

POST data:

<?xml version="1.0"?>
<ModelA>
 ...
</ModelA>
<ModelB>
 ...
</ModelB>

does not seem to be recognized. Any ideas why?

EDIT: The error data is as follows:

<?xml version="1.0"?>
<Error>
  <Message>An error has occurred.</Message>
  <ExceptionMessage>Can't bind multiple parameters ('model1' and 'model2') to the request content.
  </ExceptionMessage>
</Error>
+4
source share
4 answers

, , , : ASP.NET Web API MVC, .

: 1 , 1 , 1 .

, DTO. 2 2 DTO, DTO:

// You don't need [FromBody] since complex types are already taken
// from the request body
public IHttpActionResult DoStuff(SomeDto dto)
+6

ViewModel, .

public class MergedModel
{
    public ModelA A{get; set;}
    public ModelB B{get; set;}
}
+4

. , FromBody, . , FromBody.

, :

+3
source

You can use stringify for JSON data.

 JSON.stringify({ ModelA: o, ModelB: a}),

Why are you using XML when it is heavily loaded?

+2
source

All Articles