Extending the ASP.NET MVC HtmlHelper in VB.NET Specifically

 

I’ve been reading up on MVC and understanding it’s framework. Of course everything out there is in C#, which is fine, however I prefer to write in VB.Net.  This causes some of the examples of lambda expressions and other things to alter, and can be a pain if you are unsure has how.  

This post is specific to the HtmlHelper, and extending it on MVC.

In mvc, you have access to an html. namespace, for links and other html controls.

  • Html.ActionLink()
  • Html.BeginForm()
  • Html.CheckBox()
  • Html.DropDownList()
  • Html.EndForm()
  • Html.Hidden()
  • Html.ListBox()
  • Html.Password()
  • Html.RadioButton()
  • Html.TextArea()
  • Html.TextBox()

 

If you want to know more about it you can read up on it on various posts, such as this here or here, or use it in the mvc music store tutorial. That will give you an good understanding of it.

This post will just show you the difference from the c# examples.   

In C#

Code Snippet
  1. using System.Web.Mvc;
  2. namespace MvcMusicStore.Helpers
  3. {
  4.     public static class HtmlHelpers
  5.     {
  6.         public static string Truncate(this HtmlHelper helper, string input, int length)
  7.         {
  8.             if (input.Length <= length)
  9.             {
  10.                 return input;
  11.             }
  12.             else
  13.             {
  14.                 return input.Substring(0, length) + "...";
  15.             }
  16.         }
  17.     }
  18. }

 

And then in VB.Net

Code Snippet
  1. Imports System.Web.Mvc
  2. Public Module HtmlHelpers
  3.     <System.Runtime.CompilerServices.Extension>    
  4.     Public Function Truncate(Helper As HtmlHelper, Input As String, Length As Integer) As String
  5.         If Input.Length <= Length Then
  6.             Return Input
  7.         Else
  8.             Return Input.Substring(0, Length) & "..."
  9.         End If
  10.     End Function
  11. End Module

You’ll notice that in the C# version it uses a “Static” keyword on the Class. In VB that equivalent keyword is “Shared”.  However, in VB you can’t use the “Shared” keyword on a Class. VB only supports “Shared” on Methods. So you should use a Module instead of a Class which is the same thing actually. It’s from the old VB days.

For your function you have to add the attribute

<System.Runtime.CompilerServices.Extension> 

Notice I don’t have the underscore _ line extending, because I’m in VB.Net 10. Previous versions or .Net 3.5 and earlier you’ll need an underscore to connect those two lines.

Other then that it’s about the same. Still have to add it to the web.config the same. 

Code Snippet
  1. <pages>
  2.   <namespaces>
  3.     <add namespace="System.Web.Mvc" />
  4.     <add namespace="System.Web.Mvc.Ajax" />
  5.     <add namespace="System.Web.Mvc.Html" />
  6.     <add namespace="System.Web.Routing" />
  7.     <add namespace="MvcMusicStore.HtmlHelpers"/>  
  8.   </namespaces>
  9. </pages>

 

So just remember, use a module instead of class, and add your attribute.

Delete All Files & Folders from Parent that are older then X Days

Control Your Windows Service from a Asp.NET Web App, Start, Stop, Run A Method