0

how do I get asp.net webapi to look at the route data and body to bind to a complex object?

Using the following route "api/products/{productid}/manufacturers/{manufacturerId}" I need the productId and manufacturerId to bind to a model, so my controller method is as

public IHttpActionResult Create(CreateProductManufacturerModel
                createProductManufacturerModel)

and my model is

 public class CreateProductManufacturerModel
    {
        public int ProductId { get; set; }
        public int ManufacturerId { get; set; }
        public bool IsDefault { get; set; }
        public string ManufacturerCode { get; set; }
        public string Description { get; set; }
    }

I know I could change my method to be as below, but I am using fluentvalidation to validate the whole createproductmanufacturermodel, this is done automatically (see- http://www.justinsaraceno.com/2014/07/fluentvalidation-with-webapi2/). So the productId and manufacturerId would not be validated correctly as the are set as zero.

   public IHttpActionResult Create(int productId, int manufacturerId, CreateProductManufacturerModel
                createProductManufacturerModel)

I've have tried a modelbinder but it then does not fire the fluentvalidation automatically. Not too sure if this matters, but the body being posted is in a json format.

Thanks in advance.

Paul

2
  • Why cant you add productId and manufacturerId in createproductmanufacturermodel with other values in json data ? Commented Nov 28, 2016 at 14:12
  • you could, but that makes me feel dirty, as the data is supplied twice Commented Nov 28, 2016 at 14:24

2 Answers 2

1
  1. Inherit System.Web.Http.Filters.ActionFilterAttribute.
  2. Override OnActionExecuting()
  3. Extract routing data using actionContext.RequestContext.RouteData.Values
  4. Extract model using actionContext.ActionArguments
  5. Validate and assign routing data to model properties
  6. Decorate your action with the new attribute you created.

If this is a less specific use case, you can use reflection to assign routing data to model properties according to param names.

Sign up to request clarification or add additional context in comments.

Comments

0

Can you change your action signature to be:

public IHttpActionResult Create([FromBody]CreateProductManufacturerModel
            createProductManufacturerModel){}

and then validate the 2 values you need: createProductManufacturerModel.ProductId and createProductManufacturerModel.ManufacturerId ?

1 Comment

I'm currently using the method with the productId, manufacturerId and createProductManufacturerModel that I mentioned. As I currently have fluentvalidation firing the validation automatically. I would have two set of validations :(

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.