Using Functions and Helpers in ASP.NET Web Pages - WebMatrix

Posted by: Malcolm Sheridan , on 1/24/2011, in Category ASP.NET
Views: 117481
Abstract: The following article demonstrates how to use functions with helpers when you’re using ASP.NET Web Pages.

A few weeks ago I wrote an article on how to use helpers when you're using ASP.NET Web Pages. That article was written when WebMatrix was in Beta, but now it's released I want to share a little extra piece of functionality that I didn't in the previous article. Helpers are written in either Razor syntax, or even by creating class libraries in C# or VB.NET. In addition to helpers there are functions. Functions aid your helpers so you Don't Repeat Yourself (DRY) when creating helpers. Functions are defined by the functions keyword, and they're great if you want to maximize your knowledge.

Before you begin you need to install the latest version of WebMatrix. The current release can be installed easily through the Web Platform Installer. Once the install is finished you’re ready to begin.


Helpers reduce the code you need to write by implementing the Don’t Repeat Yourself (DRY) principal. For example I'm going to create a helper that either reduces or increase the cost of an item depending on what day of the week it is. Because this code can be used on multiple pages, I’m going to create a helper method. Helpers and functions go together hand in hand to reduce the code you might normally duplicate.

Helper methods can be created inside of WebMatrix, or alternatively they can be created in Visual Studio and referenced by your WebMatrix project.

Creating Helpers in Webmatrix

If you create Helpers inside of WebMatrix, you must first create a new folder called App_Code.

WebMatrix App Code

Any files inside of App_Code are compiled at runtime. The next step is to create the Helper. Start by adding a new cshtml or vbhtml file to the App_Code folder. For this example I’m going to create a Helper called PriceHelper. This file can be called anything you like. For common code inside helpers, you can create static methods and wrap these inside of functions. Functions are defined by the @functions keyword. Take a look at the code below.

WebMatrix Function Helpers

I’ve created three functions, GetPriceDetails, GetDayAsNumber and GetDay. These functions reduce the duplicate code I would otherwise have in my helper method SetPrice. Functions must follow two rules:

  1. The must reside in the @functions code block
  2. The method must be static

If you make the function access modifiers public, then you could call these functions directly from your page. I would recommend not doing this as functions are supposed to be used by helpers, not directly from other areas of your code.

Know them, love them and use them!

The entire source code of this article can be downloaded over here

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Malcolm Sheridan is a Microsoft awarded MVP in ASP.NET, a Telerik Insider and a regular presenter at conferences and user groups throughout Australia and New Zealand. Being an ASP.NET guy, his focus is on web technologies and has been for the past 10 years. He loves working with ASP.NET MVC these days and also loves getting his hands dirty with jQuery and JavaScript. He also writes technical articles on ASP.NET for SitePoint and other various websites. Follow him on twitter @malcolmsheridan

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Stephen on Monday, January 24, 2011 6:53 AM
Funny. I was just thinking about helpers a while back. Is ther eany other way to install webmatrix other than using the Web Platform Installer?
Comment posted by Artem Lang on Monday, January 24, 2011 6:57 AM
Very nice concept on helpers. Can a webmatrix app can be upgraded to MVC?
Comment posted by Malcolm Sheridan on Monday, January 24, 2011 5:22 PM
The web platform installer is the easy way :)

@Artem Lang
I don't believe there's currently an automatic way of upgrading.  This may change in the future.
Comment posted by jairo on Friday, May 27, 2011 12:00 PM
My friend, i think that u miss a little detail: later of write the helper, how I can use It?
Comment posted by sanj on Monday, May 28, 2012 5:45 PM
Quick question, so if I add several functions (those I use regularly in several other pages) to a page that resides in the app_code it will be compiled - thereby I don't need add them to the code behind pages and use them directly on my pages as <%# myFunction(Eval("somefield"))%>