2

I need to create an end point, but this endpoint can have multiple types of input, the form itself can change based on configs, so i was trying to create at least 2 objects as possible inputs.

Something like:

public class ParticipationsController : ApiController
{

 public HttpResponseMessage Post([FromBody]Models.SimpleParticipationModel sModel, [FromBody]Models.CompleteParticipationModel cModel)
    {
        if (!ModelState.IsValid) // this might not be this way here 
        {
            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }

        return Request.CreateResponse(HttpStatusCode.OK, "Ok");
    }

My point is to avoid having multiple endpoints and change a lot of renderization in the page.

My objects contain DataAnotations to obey certain rules such as "Required" and "Range 0-X".

I also didn't what to have an object with all the properties, and only fulfill some of them.

Thanks in advance

1 Answer 1

1

This is probably not possible. Either create two end points for each of the object or create object which include both of the above objects.

For example, here you can pass a object of ViewModel in the API action which basically includes both the objects. This will also maintain your Data Annotation behaviour over the object properties.

public class ViewModel
{ 
     SimpleParticipationModel  sModel {get;set;}
     CompleteParticipationModel  cModel {get;set;}
}

public class ParticipationsController : ApiController
{

 public HttpResponseMessage Post([FromBody]ViewModel)
 {
    if (!ModelState.IsValid) // this might not be this way here 
    {
        return Request.CreateResponse(HttpStatusCode.BadRequest);
    }

    return Request.CreateResponse(HttpStatusCode.OK, "Ok");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.