Creating an External Helper Method
Chapter 25 showed you how to use the @helper
tag to create an internal helper. These are a useful way to avoid repetition, but they can be used only in the view in which they are defined, and they have a tendency to take over a view if they involve any significant amount of code.
The alternative is to create an external helper, which takes the form of a C# extension method. Listing 26-1 shows an example of an external helper that I have placed in a class called ExternalHelpers
in the Infrastructure
project folder. The MVC framework contains a set of built-in helper methods that cover most common requirements, but understanding how to build your own helps put everything in context.
Tip Helper methods are intended ...
Get Applied ASP.NET 4 in Context now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.