Skip to content
/ Epoxy Public
forked from GokGokalp/Epoxy

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

Notifications You must be signed in to change notification settings

ucatal/Epoxy

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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; }
}

About

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

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%