Services.AddSingleton, ConfigureMvcOptions>() Services.AddControllers().AddNewtonsoftJson() Public static IServiceCollection ConfigureMvc( this IServiceCollection services) If you are using or Newtonsoft Json.NET for model serialization, you could also configure them in a similar manner. ![]() The goal is to call AddControllers(), AddControllersWithViews() or AddRazorPages(), based on your scenario, and define configuration for MvcOptions, and possibly RazorPagesOptions. Whether you are using Controllers only, or including Views or Razor Pages, MVC setup is more or less the same. And, since all IOptions instances are part of service container, we can use IConfigureOptions or IPostConfigureOptions to override any one of them. One design standard, to keep in mind, is that almost all parts of ASP.NET Core are configured via IOptions pattern. Similar to how ASP.NET Core uses plugin architecture, we will utilize these features to separate concerns and leave heavy lifting to dependency injection container. NET features: Options pattern and Extension methods. Groves for letting me participate! Basic principles This post is part of C# Advent Calendar 2021. Despite that fact, I am sure the ideas presented in this post will be relevant, even in the new era. ASP.NET Core 6 has rolled in and removed the need of having Startup.cs altogether. I wanted to show you a way to prevent startup from growing uncontrollably, by keeping different concerns separate from each other. Although complete application setup can be packed into a single type, we must not forget about Single-responsibility principle. A well-established way of doing this is using Startup.cs and its ConfigureServices() and Configure() methods. ![]() Configuring service container and setting up request pipeline in ASP.NET Core can eat up a lot of lines of code, especially for more complex projects.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |