Logo
25 Mar 2015 | 1 min. (117 words)

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.

app_code asp.net asp.net-mvc helper razor
Twitter Facebook

Setting up a simple AngularJS app to display an array

How to display an array of data using AngularJs…

Managing @font-face definitions with LESS

…

Related Links

  • LinkedIn
  • Twitter: @curtcode
  • Stack Overflow
  • GitHub: @curtiscde

Stack Exchange

profile for Curtis on Stack Exchange, a network of free, community-driven Q&A sites
Follow @curtiscde

Recent Posts

  • My terminal setup and commands I use on a daily basis when working with GitHub
  • Displaying latest posts on your GitHub profile
  • Using Codecov within a monorepo
  • Displaying Strava stats using webhooks & GitHub Actions
  • Filtering duplicate data points on Chart.js
Theme Bleak by zutrinken Published with Hugo
Menu