How to store razor helpers globally
ASP.NET MVC Razor helpers are a great way of reducing duplicated code by containing code snippets in a View which can then be called multiple times.
<div>
@DisplayGreeting("Curtis")
</div>
@helper DisplayGreeting(string name)
{
<span>Hello @name</span>
}
However what if you wish to call this same helper on another View?
In which case the helper can be stored in a seperate View in the "App_Code" folder.
For example create a new View in the "App_Code" folder called "MyHelpers.cshtml":
Move the helper code into this file:
Then the helper can be called from the previous view by using MyHelpers.DisplayGreeting
:
<div>
@MyHelpers.DisplayGreeting("Curtis")
</div>
This way any View within the same ASP.NET MVC Application can then make use of this helper.