ASP.NET Web Pages - Helpers

Helpers in ASP.NET Web Pages allow code reuse. Helpers are methods and can take arguments, the return type for a helper is HelperResult. With helpers you can include C# and HTML code. In the next exercise we will create simple helpers: one will not pass any arguments while the second one will. Create a new website based of the Bakery template, and name the website Helpers.

Helper files must be placed in the App_Code folder as this is a secure folder. Create a new folder App_Code and inside that create a cshtml file named Helpers.

Sponsored Links

Syntax

@helper helpername ( )
{

}

Example

In the Helpers file place this code:

@helper myDiv (){

<div style="width: 250px;height: 500px;background-color: #ff6a00;padding: 20px;color: #fff;">

@DateTime.Now

</div>

}

Next in the root directory make a new .cshtml and call it Sample.cshtml and replace everything with this:

@Helpers.myDiv()

Run the Sample.cshtml in the browser and you should see an Orange div with the date and time. Here you can see how you can mix HTML and C# code together. When you call a helper you start with the Helpers keyword followed by a dot (period), the helper name and brackets.

Passing Arguments

If you want to make the most out of helpers and code reuse you need to know how to pass arguments. Arguments are basically values you supply when you call that method.

Syntax

@helper helpername (argument 1, argument 2)

{

}

Example

@helper myGames (string[] games)
{
<ul>
    @foreach (var g in games)
    {
        <li>@g</li>
    }
</ul>
}

In this example we have an array named games which is a parameter, and we have a un-ordered list and a foreach statement which will print out the values in the array gamesTo call this method we must specify the arguments (arguments are the values you specify, parameters are the names given in the methods). 

This will not work:

@Helpers.myGames ()<!--Compile Time Error // No Arguments No overload for method 'myGames' takes 0 arguments-->

You must specify the arguments like this:

@
{
    string[] games = {"Infamous", "Uncharted", "GTA5", "NFS", "Fifa"};
}

Here we created a string array with our values.

Call the helper:

@Helpers.myGames(games)

Here we made a string array of games (remember: in the method the parameter was an array, so the data type must match). When you run this in the browser you should see the games print out:

  • Infamous
  • Uncharted
  • GTA5
  • NFS
  • Fifa

Optional Parameters

It is possible to have optional parameters, which must go at the end. To create an optional parameter you use the equal sign followed by the default value. In the following example we will loop through some numbers specified by the user.

Example

@helper myNumbers (int startpoint, int endpoint = 50)
{
<ul>
@for (int i = startpoint; i < endpoint; i++)
{
    <li>@i</li>
}
</ul>  
}

This will simply create an unordered list of numbers from the starting point which you will specify and the end point. The default value for the end point is 50, however this is optional and the user does not need to specify the end point.

To call this you can specify the first argument only:

@Helpers.myNumbers(1)

This will print out numbers 1 – 50.

Our you can pass both arguments:

@Helpers.myNumbers(1, 20)

This will print out numbers 1 – 20.

Overloading Methods

If you have programmed in the C# language before you will understand overloading methods. If you have not covered C# please see the tutorials. It is possible to overload methods using helpers in ASP.NET Web Pages.  In the next example we will overload a method, which simply prints out values of different data types.

@helper methodOverloading (string name)
{
@name  
}
@helper methodOverloading (int num)
{
@num
}

This is a very simple overloaded method: the method names are the same but it takes different parameters and data types.

In the Samples put:

@Helpers.methodOverloading("asim")

You can also provide a number like this:

@Helpers.methodOverloading(10)

When you begin to type intellisense will show you the arguments this method takes, as seen in the image below:

ASP.NET Web Pages - Helpers Method Overloading

Summary

This tutorial covered helpers, which can be extremely useful for code reuse.

  • Helpers can contain HTML and Server Code
  • Helpers are methods
  • Helpers return HelperResult