diff --git a/aspnetcore/mvc/advanced/custom-model-binding.md b/aspnetcore/mvc/advanced/custom-model-binding.md index b3e0c55d5b3f..c9474cd42832 100644 --- a/aspnetcore/mvc/advanced/custom-model-binding.md +++ b/aspnetcore/mvc/advanced/custom-model-binding.md @@ -3,7 +3,7 @@ title: Custom Model Binding in ASP.NET Core author: ardalis description: Learn how model binding allows controller actions to work directly with model types in ASP.NET Core. ms.author: riande -ms.date: 12/05/2019 +ms.date: 01/01/2020 uid: mvc/advanced/custom-model-binding --- # Custom Model Binding in ASP.NET Core @@ -22,21 +22,13 @@ The default model binders support most of the common .NET Core data types and sh Model binding uses specific definitions for the types it operates on. A *simple type* is converted from a single string in the input. A *complex type* is converted from multiple input values. The framework determines the difference based on the existence of a `TypeConverter`. We recommended you create a type converter if you have a simple `string` -> `SomeType` mapping that doesn't require external resources. -Before creating your own custom model binder, it's worth reviewing how existing model binders are implemented. Consider the [ByteArrayModelBinder](/dotnet/api/microsoft.aspnetcore.mvc.modelbinding.binders.bytearraymodelbinder) which can be used to convert base64-encoded strings into byte arrays. The byte arrays are often stored as files or database BLOB fields. +Before creating your own custom model binder, it's worth reviewing how existing model binders are implemented. Consider the which can be used to convert base64-encoded strings into byte arrays. The byte arrays are often stored as files or database BLOB fields. ### Working with the ByteArrayModelBinder -Base64-encoded strings can be used to represent binary data. For example, the following image can be encoded as a string. +Base64-encoded strings can be used to represent binary data. For example, an image can be encoded as a string. Follow the instructions in the [sample's README](https://github.com/aspnet/AspNetCore.Docs/blob/master/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/README.md) to convert a base64-encoded string into a file. -![dotnet bot](custom-model-binding/images/bot.png "dotnet bot") - -A small portion of the encoded string is shown in the following image: - -![dotnet bot encoded](custom-model-binding/images/encoded-bot.png "dotnet bot encoded") - -Follow the instructions in the [sample's README](https://github.com/aspnet/AspNetCore.Docs/blob/master/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/README.md) to convert the base64-encoded string into a file. - -ASP.NET Core MVC can take a base64-encoded string and use a `ByteArrayModelBinder` to convert it into a byte array. The [ByteArrayModelBinderProvider](/dotnet/api/microsoft.aspnetcore.mvc.modelbinding.binders.bytearraymodelbinderprovider) which implements [IModelBinderProvider](/dotnet/api/microsoft.aspnetcore.mvc.modelbinding.imodelbinderprovider) maps `byte[]` arguments to `ByteArrayModelBinder`: +ASP.NET Core MVC can take a base64-encoded string and use a `ByteArrayModelBinder` to convert it into a byte array. The maps `byte[]` arguments to `ByteArrayModelBinder`: ```csharp public IModelBinder GetBinder(ModelBinderProviderContext context) @@ -55,11 +47,11 @@ public IModelBinder GetBinder(ModelBinderProviderContext context) } ``` -When creating your own custom model binder, you can implement your own `IModelBinderProvider` type, or use the [ModelBinderAttribute](/dotnet/api/microsoft.aspnetcore.mvc.modelbinderattribute). +When creating your own custom model binder, you can implement your own `IModelBinderProvider` type, or use the . The following example shows how to use `ByteArrayModelBinder` to convert a base64-encoded string to a `byte[]` and save the result to a file: -[!code-csharp[](custom-model-binding/sample/CustomModelBindingSample/Controllers/ImageController.cs?name=post1&highlight=3)] +[!code-csharp[](custom-model-binding/samples/2.x/CustomModelBindingSample/Controllers/ImageController.cs?name=post1&highlight=3)] You can POST a base64-encoded string to this api method using a tool like [Postman](https://www.getpostman.com/): @@ -67,7 +59,7 @@ You can POST a base64-encoded string to this api method using a tool like [Postm As long as the binder can bind request data to appropriately named properties or arguments, model binding will succeed. The following example shows how to use `ByteArrayModelBinder` with a view model: -[!code-csharp[](custom-model-binding/sample/CustomModelBindingSample/Controllers/ImageController.cs?name=post2&highlight=2)] +[!code-csharp[](custom-model-binding/samples/2.x/CustomModelBindingSample/Controllers/ImageController.cs?name=post2&highlight=2)] ## Custom model binder sample @@ -79,24 +71,24 @@ In this section we'll implement a custom model binder that: The following sample uses the `ModelBinder` attribute on the `Author` model: -[!code-csharp[](custom-model-binding/sample/CustomModelBindingSample/Data/Author.cs?highlight=10)] +[!code-csharp[](custom-model-binding/samples/2.x/CustomModelBindingSample/Data/Author.cs?highlight=6)] In the preceding code, the `ModelBinder` attribute specifies the type of `IModelBinder` that should be used to bind `Author` action parameters. The following `AuthorEntityBinder` class binds an `Author` parameter by fetching the entity from a data source using Entity Framework Core and an `authorId`: -[!code-csharp[](custom-model-binding/sample/CustomModelBindingSample/Binders/AuthorEntityBinder.cs?name=demo)] +[!code-csharp[](custom-model-binding/samples/2.x/CustomModelBindingSample/Binders/AuthorEntityBinder.cs?name=demo)] > [!NOTE] > The preceding `AuthorEntityBinder` class is intended to illustrate a custom model binder. The class isn't intended to illustrate best practices for a lookup scenario. For lookup, bind the `authorId` and query the database in an action method. This approach separates model binding failures from `NotFound` cases. The following code shows how to use the `AuthorEntityBinder` in an action method: -[!code-csharp[](custom-model-binding/sample/CustomModelBindingSample/Controllers/BoundAuthorsController.cs?name=demo2&highlight=2)] +[!code-csharp[](custom-model-binding/samples/2.x/CustomModelBindingSample/Controllers/BoundAuthorsController.cs?name=demo2&highlight=2)] The `ModelBinder` attribute can be used to apply the `AuthorEntityBinder` to parameters that don't use default conventions: -[!code-csharp[](custom-model-binding/sample/CustomModelBindingSample/Controllers/BoundAuthorsController.cs?name=demo1&highlight=2)] +[!code-csharp[](custom-model-binding/samples/2.x/CustomModelBindingSample/Controllers/BoundAuthorsController.cs?name=demo1&highlight=2)] In this example, since the name of the argument isn't the default `authorId`, it's specified on the parameter using the `ModelBinder` attribute. Both the controller and action method are simplified compared to looking up the entity in the action method. The logic to fetch the author using Entity Framework Core is moved to the model binder. This can be a considerable simplification when you have several methods that bind to the `Author` model. @@ -106,24 +98,16 @@ You can apply the `ModelBinder` attribute to individual model properties (such a Instead of applying an attribute, you can implement `IModelBinderProvider`. This is how the built-in framework binders are implemented. When you specify the type your binder operates on, you specify the type of argument it produces, **not** the input your binder accepts. The following binder provider works with the `AuthorEntityBinder`. When it's added to MVC's collection of providers, you don't need to use the `ModelBinder` attribute on `Author` or `Author`-typed parameters. -[!code-csharp[](custom-model-binding/sample/CustomModelBindingSample/Binders/AuthorEntityBinderProvider.cs?highlight=17-20)] +[!code-csharp[](custom-model-binding/samples/2.x/CustomModelBindingSample/Binders/AuthorEntityBinderProvider.cs?highlight=17-20)] > Note: > The preceding code returns a `BinderTypeModelBinder`. `BinderTypeModelBinder` acts as a factory for model binders and provides dependency injection (DI). The `AuthorEntityBinder` requires DI to access EF Core. Use `BinderTypeModelBinder` if your model binder requires services from DI. To use a custom model binder provider, add it in `ConfigureServices`: -[!code-csharp[](custom-model-binding/sample/CustomModelBindingSample/Startup.cs?name=callout&highlight=5-9)] - -When evaluating model binders, the collection of providers is examined in order. The first provider that returns a binder is used. - -The following image shows the default model binders from the debugger. - -![default model binders](custom-model-binding/images/default-model-binders.png "default model binders") - -Adding your provider to the end of the collection may result in a built-in model binder being called before your custom binder has a chance. In this example, the custom provider is added to the beginning of the collection to ensure it's used for `Author` action arguments. +[!code-csharp[](custom-model-binding/samples/2.x/CustomModelBindingSample/Startup.cs?name=snippet_ConfigureServices&highlight=5-10)] -[!code-csharp[](custom-model-binding/sample/CustomModelBindingSample/Startup.cs?name=callout&highlight=5-9)] +When evaluating model binders, the collection of providers is examined in order. The first provider that returns a binder is used. Adding your provider to the end of the collection may result in a built-in model binder being called before your custom binder has a chance. In this example, the custom provider is added to the beginning of the collection to ensure it's used for `Author` action arguments. ### Polymorphic model binding @@ -142,4 +126,4 @@ Custom model binders: - Shouldn't attempt to set status codes or return results (for example, 404 Not Found). If model binding fails, an [action filter](xref:mvc/controllers/filters) or logic within the action method itself should handle the failure. - Are most useful for eliminating repetitive code and cross-cutting concerns from action methods. -- Typically shouldn't be used to convert a string into a custom type, a [TypeConverter](/dotnet/api/system.componentmodel.typeconverter) is usually a better option. +- Typically shouldn't be used to convert a string into a custom type, a is usually a better option. diff --git a/aspnetcore/mvc/advanced/custom-model-binding/images/bot.png b/aspnetcore/mvc/advanced/custom-model-binding/images/bot.png deleted file mode 100644 index 0d65dd0e3565..000000000000 Binary files a/aspnetcore/mvc/advanced/custom-model-binding/images/bot.png and /dev/null differ diff --git a/aspnetcore/mvc/advanced/custom-model-binding/images/default-model-binders.png b/aspnetcore/mvc/advanced/custom-model-binding/images/default-model-binders.png deleted file mode 100644 index f6ebd4efdb16..000000000000 Binary files a/aspnetcore/mvc/advanced/custom-model-binding/images/default-model-binders.png and /dev/null differ diff --git a/aspnetcore/mvc/advanced/custom-model-binding/images/encoded-bot.png b/aspnetcore/mvc/advanced/custom-model-binding/images/encoded-bot.png deleted file mode 100644 index 11ec46537304..000000000000 Binary files a/aspnetcore/mvc/advanced/custom-model-binding/images/encoded-bot.png and /dev/null differ diff --git a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/.bowerrc b/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/.bowerrc deleted file mode 100644 index 6406626abfef..000000000000 --- a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/.bowerrc +++ /dev/null @@ -1,3 +0,0 @@ -{ - "directory": "wwwroot/lib" -} diff --git a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/CustomModelBindingSample.csproj b/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/CustomModelBindingSample.csproj deleted file mode 100644 index a072d9c97868..000000000000 --- a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/CustomModelBindingSample.csproj +++ /dev/null @@ -1,21 +0,0 @@ - - - - netcoreapp1.0 - - - - $(PackageTargetFallback);portable-net45+win8+wp8+wpa81; - - - - - - - - - - - - - diff --git a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Program.cs b/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Program.cs deleted file mode 100644 index 7985a2c3159d..000000000000 --- a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Program.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Hosting; - -namespace CustomModelBindingSample -{ - public class Program - { - public static void Main(string[] args) - { - var host = new WebHostBuilder() - .UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseIISIntegration() - .UseStartup() - .UseApplicationInsights() - .Build(); - - host.Run(); - } - } -} diff --git a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Views/Shared/Error.cshtml b/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Views/Shared/Error.cshtml deleted file mode 100644 index e514139c454a..000000000000 --- a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Views/Shared/Error.cshtml +++ /dev/null @@ -1,14 +0,0 @@ -@{ - ViewData["Title"] = "Error"; -} - -

Error.

-

An error occurred while processing your request.

- -

Development Mode

-

- Swapping to Development environment will display more detailed information about the error that occurred. -

-

- Development environment should not be enabled in deployed applications, as it can result in sensitive information from exceptions being displayed to end users. For local debugging, development environment can be enabled by setting the ASPNETCORE_ENVIRONMENT environment variable to Development, and restarting the application. -

diff --git a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Views/Shared/_Layout.cshtml b/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Views/Shared/_Layout.cshtml deleted file mode 100644 index 6cbfb9d41493..000000000000 --- a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Views/Shared/_Layout.cshtml +++ /dev/null @@ -1,73 +0,0 @@ -@inject Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet JavaScriptSnippet - - - - - - @ViewData["Title"] - CustomModelBindingSample - - - - - - - - - - @Html.Raw(JavaScriptSnippet.FullScript) - - - -
- @RenderBody() -
-
-

© 2017 - CustomModelBindingSample

-
-
- - - - - - - - - - - - - @RenderSection("Scripts", required: false) - - diff --git a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Views/Shared/_ValidationScriptsPartial.cshtml b/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Views/Shared/_ValidationScriptsPartial.cshtml deleted file mode 100644 index 27e0ea7ce2f1..000000000000 --- a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Views/Shared/_ValidationScriptsPartial.cshtml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - diff --git a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Views/_ViewImports.cshtml b/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Views/_ViewImports.cshtml deleted file mode 100644 index 2aeabedc2c80..000000000000 --- a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Views/_ViewImports.cshtml +++ /dev/null @@ -1,2 +0,0 @@ -@using CustomModelBindingSample -@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers diff --git a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Views/_ViewStart.cshtml b/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Views/_ViewStart.cshtml deleted file mode 100644 index a5f10045db97..000000000000 --- a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Views/_ViewStart.cshtml +++ /dev/null @@ -1,3 +0,0 @@ -@{ - Layout = "_Layout"; -} diff --git a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/bower.json b/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/bower.json deleted file mode 100644 index b07e3cc5ae5d..000000000000 --- a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/bower.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "name": "asp.net", - "private": true, - "dependencies": { - "bootstrap": "3.3.7", - "jquery": "2.2.0", - "jquery-validation": "1.14.0", - "jquery-validation-unobtrusive": "3.2.6" - } -} diff --git a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/bundleconfig.json b/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/bundleconfig.json deleted file mode 100644 index 6d3f9a57aea2..000000000000 --- a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/bundleconfig.json +++ /dev/null @@ -1,24 +0,0 @@ -// Configure bundling and minification for the project. -// More info at https://go.microsoft.com/fwlink/?LinkId=808241 -[ - { - "outputFileName": "wwwroot/css/site.min.css", - // An array of relative input file paths. Globbing patterns supported - "inputFiles": [ - "wwwroot/css/site.css" - ] - }, - { - "outputFileName": "wwwroot/js/site.min.js", - "inputFiles": [ - "wwwroot/js/site.js" - ], - // Optionally specify minification options - "minify": { - "enabled": true, - "renameLocals": true - }, - // Optionally generate .map file - "sourceMap": false - } -] diff --git a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Base64String.txt b/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Base64String.txt similarity index 100% rename from aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Base64String.txt rename to aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Base64String.txt diff --git a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Binders/AuthorEntityBinder.cs b/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Binders/AuthorEntityBinder.cs similarity index 79% rename from aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Binders/AuthorEntityBinder.cs rename to aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Binders/AuthorEntityBinder.cs index 0d2791ec47a7..7708e81d3480 100644 --- a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Binders/AuthorEntityBinder.cs +++ b/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Binders/AuthorEntityBinder.cs @@ -9,6 +9,7 @@ namespace CustomModelBindingSample.Binders public class AuthorEntityBinder : IModelBinder { private readonly AppDbContext _db; + public AuthorEntityBinder(AppDbContext db) { _db = db; @@ -22,18 +23,16 @@ public Task BindModelAsync(ModelBindingContext bindingContext) } var modelName = bindingContext.ModelName; - + // Try to fetch the value of the argument by name - var valueProviderResult = - bindingContext.ValueProvider.GetValue(modelName); + var valueProviderResult = bindingContext.ValueProvider.GetValue(modelName); if (valueProviderResult == ValueProviderResult.None) { return Task.CompletedTask; } - bindingContext.ModelState.SetModelValue(modelName, - valueProviderResult); + bindingContext.ModelState.SetModelValue(modelName, valueProviderResult); var value = valueProviderResult.FirstValue; @@ -43,13 +42,12 @@ public Task BindModelAsync(ModelBindingContext bindingContext) return Task.CompletedTask; } - int id = 0; - if (!int.TryParse(value, out id)) + if (!int.TryParse(value, out var id)) { // Non-integer arguments result in model state errors bindingContext.ModelState.TryAddModelError( - modelName, - "Author Id must be an integer."); + modelName, "Author Id must be an integer."); + return Task.CompletedTask; } diff --git a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Binders/AuthorEntityBinderProvider.cs b/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Binders/AuthorEntityBinderProvider.cs similarity index 100% rename from aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Binders/AuthorEntityBinderProvider.cs rename to aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Binders/AuthorEntityBinderProvider.cs diff --git a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Controllers/AuthorsController.cs b/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Controllers/AuthorsController.cs similarity index 85% rename from aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Controllers/AuthorsController.cs rename to aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Controllers/AuthorsController.cs index d5d4d637c17a..0e0e26eeca3c 100644 --- a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Controllers/AuthorsController.cs +++ b/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Controllers/AuthorsController.cs @@ -3,25 +3,27 @@ namespace CustomModelBindingSample.Controllers { - [Produces("application/json")] + [ApiController] [Route("api/[controller]")] public class AuthorsController : Controller { private readonly AppDbContext _db; + public AuthorsController(AppDbContext db) { _db = db; } - // GET: api/authors/1 [HttpGet("{id}")] public IActionResult GetById(int id) { var author = _db.Authors.Find(id); - if(author == null) + + if (author == null) { return NotFound(); } + return Ok(author); } } diff --git a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Controllers/BoundAuthorsController.cs b/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Controllers/BoundAuthorsController.cs similarity index 62% rename from aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Controllers/BoundAuthorsController.cs rename to aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Controllers/BoundAuthorsController.cs index 532ee19ef9f5..77c25f54a668 100644 --- a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Controllers/BoundAuthorsController.cs +++ b/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Controllers/BoundAuthorsController.cs @@ -1,35 +1,34 @@ using Microsoft.AspNetCore.Mvc; using CustomModelBindingSample.Data; -using System.Linq; namespace CustomModelBindingSample.Controllers { - [Produces("application/json")] + [ApiController] [Route("api/[controller]")] - public class BoundAuthorsController : Controller + public class BoundAuthorsController : ControllerBase { - // GET: api/boundauthors/1 #region demo1 [HttpGet("{id}")] - public IActionResult GetById([ModelBinder(Name = "id")]Author author) + public IActionResult GetById([ModelBinder(Name = "id")] Author author) { if (author == null) { return NotFound(); } - if (!ModelState.IsValid) - { - return BadRequest(ModelState); - } + return Ok(author); } #endregion - // GET: api/boundauthors/get/1 #region demo2 [HttpGet("get/{authorId}")] public IActionResult Get(Author author) { + if (author == null) + { + return NotFound(); + } + return Ok(author); } #endregion diff --git a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Controllers/ImageController.cs b/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Controllers/ImageController.cs similarity index 76% rename from aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Controllers/ImageController.cs rename to aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Controllers/ImageController.cs index 186b1addb855..8261e63b4fc1 100644 --- a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Controllers/ImageController.cs +++ b/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Controllers/ImageController.cs @@ -1,28 +1,24 @@ using System.IO; -using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; namespace CustomModelBindingSample.Controllers { - [Produces("application/json")] - [Route("api/Image")] - public class ImageController : Controller + [ApiController] + [Route("api/[controller]")] + public class ImageController : ControllerBase { - private readonly IHostingEnvironment _env; private readonly string _targetFilePath; - public ImageController(IHostingEnvironment env, IConfiguration config) + public ImageController(IConfiguration config) { - _env = env; - _targetFilePath = config.GetValue("StoredFilesPath"); + _targetFilePath = config["StoredFilesPath"]; } #region post1 - // POST: api/image [HttpPost] - public void Post(byte[] file, string filename) + public void Post([FromForm] byte[] file, string filename) { // Don't trust the file name sent by the client. Use // Path.GetRandomFileName to generate a safe random @@ -43,7 +39,7 @@ public void Post(byte[] file, string filename) #region post2 [HttpPost("Profile")] - public void SaveProfile(ProfileViewModel model) + public void SaveProfile([FromForm] ProfileViewModel model) { // Don't trust the file name sent by the client. Use // Path.GetRandomFileName to generate a safe random @@ -52,7 +48,7 @@ public void SaveProfile(ProfileViewModel model) // the sample app). var trustedFileName = Path.GetRandomFileName(); var filePath = Path.Combine(_targetFilePath, trustedFileName); - + if (System.IO.File.Exists(filePath)) { return; diff --git a/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/CustomModelBindingSample.csproj b/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/CustomModelBindingSample.csproj new file mode 100644 index 000000000000..2a54222f62d6 --- /dev/null +++ b/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/CustomModelBindingSample.csproj @@ -0,0 +1,9 @@ + + + netcoreapp2.1 + + + + + + diff --git a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Data/AppDbContext.cs b/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Data/AppDbContext.cs similarity index 86% rename from aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Data/AppDbContext.cs rename to aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Data/AppDbContext.cs index 87faae61413d..06bb6c8cc8f7 100644 --- a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Data/AppDbContext.cs +++ b/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Data/AppDbContext.cs @@ -4,9 +4,9 @@ namespace CustomModelBindingSample.Data { public class AppDbContext : DbContext { + public AppDbContext(DbContextOptions options) + : base(options) { } + public DbSet Authors { get; set; } - public AppDbContext(DbContextOptions options) : base(options) - { - } } } diff --git a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Data/Author.cs b/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Data/Author.cs similarity index 81% rename from aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Data/Author.cs rename to aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Data/Author.cs index 0bfea3ad5c48..d77d47e5d6f8 100644 --- a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Data/Author.cs +++ b/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Data/Author.cs @@ -1,9 +1,5 @@ using CustomModelBindingSample.Binders; using Microsoft.AspNetCore.Mvc; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; namespace CustomModelBindingSample.Data { diff --git a/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Program.cs b/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Program.cs new file mode 100644 index 000000000000..9e2c99dc969f --- /dev/null +++ b/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Program.cs @@ -0,0 +1,17 @@ +using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Hosting; + +namespace CustomModelBindingSample +{ + public class Program + { + public static void Main(string[] args) + { + CreateWebHostBuilder(args).Build().Run(); + } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + WebHost.CreateDefaultBuilder(args) + .UseStartup(); + } +} diff --git a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/README.md b/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/README.md similarity index 100% rename from aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/README.md rename to aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/README.md diff --git a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Startup.cs b/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Startup.cs similarity index 70% rename from aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Startup.cs rename to aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Startup.cs index 3ef1d4d012b7..d8578929865b 100644 --- a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/Startup.cs +++ b/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/Startup.cs @@ -1,6 +1,7 @@ using CustomModelBindingSample.Binders; using CustomModelBindingSample.Data; using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; @@ -8,17 +9,17 @@ namespace CustomModelBindingSample { public class Startup { - - #region callout + #region snippet_ConfigureServices public void ConfigureServices(IServiceCollection services) { - services.AddDbContext(options => options.UseInMemoryDatabase()); + services.AddDbContext(options => options.UseInMemoryDatabase("App")); services.AddMvc(options => - { - // add custom binder to beginning of collection - options.ModelBinderProviders.Insert(0, new AuthorEntityBinderProvider()); - }); + { + // add custom binder to beginning of collection + options.ModelBinderProviders.Insert(0, new AuthorEntityBinderProvider()); + }) + .SetCompatibilityVersion(CompatibilityVersion.Version_2_1); } #endregion @@ -37,4 +38,4 @@ private void PopulateTestData(AppDbContext db) db.SaveChanges(); } } -} \ No newline at end of file +} diff --git a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/appsettings.Development.json b/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/appsettings.Development.json similarity index 100% rename from aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/appsettings.Development.json rename to aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/appsettings.Development.json diff --git a/aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/appsettings.json b/aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/appsettings.json similarity index 100% rename from aspnetcore/mvc/advanced/custom-model-binding/sample/CustomModelBindingSample/appsettings.json rename to aspnetcore/mvc/advanced/custom-model-binding/samples/2.x/CustomModelBindingSample/appsettings.json