Skip to content

Latest commit

 

History

History
81 lines (65 loc) · 2.29 KB

README.md

File metadata and controls

81 lines (65 loc) · 2.29 KB

Epoxy


alt tag

Simple both fromuri and frombody model binder for Asp.NET Web API

Build status Test status NuGet version

NuGet Packages

PM> Install-Package Epoxy 

####Features:

  • Binds both fromuri and frombody data to model
  • Supports model validations
  • Currently only support JSON data for body

Usage:

Firstly insert EpoxyFromUriAndFromBodyBinderProvider to HttpConfiguration.Services:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Services.Insert(typeof(ModelBinderProvider), 0, new EpoxyFromUriAndFromBodyBinderProvider());

        // ...
    }
}

after service insert step, you should use [ModelBinder] attribute. There are two ways:

First way is: in Controller. For example:

namespace Epoxy.Tests.Host.Controllers
{
    [RoutePrefix("api/products")]
    public class ProductController : ApiController
    {
        [HttpPost, Route("{productId}/variants")]
        public IHttpActionResult AddProductsVariants([ModelBinder]AddProductVariantRequest addProductVariantRequest)
        {
            if (addProductVariantRequest.ProductId > 0)
            {
                return Ok();
            }
            
            return BadRequest();
        }
    }
}

after this, Epoxy will bind uri parameters(eg: productId) and payload data to AddProductVariantRequest object.

Second way is: in request object. For example:

[ModelBinder]
public class AddProductVariantRequest
{
    public int ProductId { get; set; }

    public string Name { get; set; }
    public decimal Price { get; set; }
    public decimal? SpecialPrice { get; set; }
    public int? SpecialPriceDuration { get; set; }
    public DateTime? SpecialPriceStartDate { get; set; }
    public DateTime CreatedOn { get; set; }

    public Category DefaultCategory { get; set; }
    public List<Category> AdditionalCategories { get; set; }
}