RonSijm.ModelRebindingFromRoute
1.0.0
Prefix Reserved
dotnet add package RonSijm.ModelRebindingFromRoute --version 1.0.0
NuGet\Install-Package RonSijm.ModelRebindingFromRoute -Version 1.0.0
<PackageReference Include="RonSijm.ModelRebindingFromRoute" Version="1.0.0" />
paket add RonSijm.ModelRebindingFromRoute --version 1.0.0
#r "nuget: RonSijm.ModelRebindingFromRoute, 1.0.0"
// Install RonSijm.ModelRebindingFromRoute as a Cake Addin #addin nuget:?package=RonSijm.ModelRebindingFromRoute&version=1.0.0 // Install RonSijm.ModelRebindingFromRoute as a Cake Tool #tool nuget:?package=RonSijm.ModelRebindingFromRoute&version=1.0.0
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.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net5.0
- Microsoft.AspNetCore.Mvc.Abstractions (>= 2.2.0)
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 363 | 5/6/2021 |