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.
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.
- using System.Web.Mvc;
- namespace MvcMusicStore.Helpers
- public static class HtmlHelpers
- public static string Truncate(this HtmlHelper helper, string input, int length)
- if (input.Length <= length)
- return input;
- return input.Substring(0, length) + "...";
And then in VB.Net
- Imports System.Web.Mvc
- Public Module HtmlHelpers
- Public Function Truncate(Helper As HtmlHelper, Input As String, Length As Integer) As String
- If Input.Length <= Length Then
- Return Input
- Return Input.Substring(0, Length) & "..."
- End If
- End Function
- 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
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.
- <add namespace="System.Web.Mvc" />
- <add namespace="System.Web.Mvc.Ajax" />
- <add namespace="System.Web.Mvc.Html" />
- <add namespace="System.Web.Routing" />
- <add namespace="MvcMusicStore.HtmlHelpers"/>
So just remember, use a module instead of class, and add your attribute.