Coder Social home page Coder Social logo

yarden-zamir / ronsijm.modelrebindingfromroute Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ronsijm/ronsijm.modelrebindingfromroute

0.0 0.0 0.0 9 KB

Library with a IModelBinder which will rebind route input into a model

License: MIT License

C# 100.00%

ronsijm.modelrebindingfromroute's Introduction

Model Rebinding From Route

This library provides an IModelBinderProvider and an IModelBinder which makes it easy to have REST-like paths, but still conserve all input data into a singular model.

Problem Example

Consider you have an application with books, and books have pages. You want to POST text to a specific page.

Traditionally your API controller would look like this:

    [HttpPost("[controller]/book/{bookId}/page/{pageId}")]
    public string Echo(string bookId, string pageId, PageContent content)

This is fine, however if your project gets bigger and bigger, you'll probably start to add custom IActionFilters which will take input objects for processing purposes.

This is where the issue starts and the Model Rebinding comes in.

Problem solution

With this library, you are able to write API controllers like so:

    [HttpPost("[controller]/book/{bookId}/page/{pageId}")]
    public string Echo(BookRequestModel bookRequestModel)

The BookRequestModel has the following implementation:

    public class BookRequestModel
    {
        [FromRoute]
        public string BookId { get; set; }

        [FromRoute]
        public string PageId { get; set; }
    }

Note that the property names must match the path names, that's how the model rebinder will know where to rebind which route value, and the properties must have an [FromRoute] attribute. Mainly for performance reasons, so that if the model is a traditional model without any rebinding properties, the default binders are used.

Usage

Install the package:

install-package RonSijm.ModelRebindingFromRoute

Wire the model rebinder into your startup like so:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers(options =>
        {
            options.ModelBinderProviders.Insert(0, new ModelBindingProviderInterceptor(options.ModelBinderProviders));
        });
    }

Note: It's important to wire it by using Insert(0) - this way the rebinder is used with as first attempt. Otherwise a default binder will pick this up, and if it's capable of binding, the rebinder is skipped entirely.

ronsijm.modelrebindingfromroute's People

Contributors

ronsijm avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.