Shawn Wildermuth

Author, Teacher, and Filmmaker
.NET Foundation Board Member


Tagged with Razor

Using Services in ASP.NET Core Razor Pages

I feel like the job of software developer in the last 20 years has been to decouple. Whether it's dependency injection or building modular systems, or even the new trend of micro-services; coupling has been the killer of everything good in software development (maybe).

In many small ways, I find that trying to fit in small disconnected sets of functionality into the ASP.NET MVC Controller to View mechanism can be overwhelming. In some cases I'll need something that is completely separate from the logic of the controller. Luckily ASP.NET Core comes to the rescue.

The ServiceContainer in ASP.NET Core is great, but we tend to worry about constructor injection in most cases. But once I realized I could inject directly into Razor, I realized I had a good solution for my needs.