The Html class was shipped with ASP.Net MCV Framework. This class provides lots of functions to render controls to the view and support you to do common tasks without writing dirty html.
Here are some of the Html class’s functions
- Html.ActionLink
- Html.CheckBox
- Html.ListBox
- Html.Password
- Html.TextBox
- Html.TextArea
The html class provides more functions than listed above but in some cases, the suggested function is not implemented.
I’ll show you two ways to extend this class for your own usage.
The simplest way to extend the html class is to write some extensionmethods which will be available for the Html Class. Perhaps you need the method RenderWhatEver which renders whatEver to html?
Here is the signature:
public static string RenderWhatEver(this HtmlHelper helper, string whatEver)
{
//..
}
There is only one thing important, Methods in the Html class must return a String. All other limitations are given by .Net Framework 3.5 extension methods.
The other opinion is to write a custom class such as MyHtmlHelper. All methods you will use within an aspx site have to be static and public, as seen above.
The Method RenderWhatEver within a custom class
public class MyHtmlHelper
{
public static String RenderWhatEver(String WhatEverParameter)
{ ... }
}
Each of these solutions can be used in any aspx site wrapped by a <%= ... %> Expression.
If you want to write your own class which provides a collections of Methods, don’t forget to import the class with its full name.