Adding HTML Helper Extensions in ASP.NET MVC

I’ve been working in ASP.NET MVC as much as possible lately, because it’s so much more enjoyable than working in WebForms. There is still some debate about its usefulness for some applications, but that is a whole separate topic. ;)

One of the things I’ve done is create some HTML helper functions. They are really useful and there are several more I think I could create. I don’t know if these are as generic as they could be, but they work pretty well for what I need them for. When I start my next MVC project I may see that they could use some revision and I’ll extract them into their own library. They do require a dependency on System.Web.Mvc.

The first function I made because I missed it from my Ruby on Rails days: LinkToUnlessCurrent. Frankly, I’m kind of surprised that this is not baked in. The second function I made when I started getting more requests for role-based authentication. Using AD as your role and auth provider makes all this really easy – you can just use the group name as your role name anywhere in MVC.

Ok, here’s the code:

        <Extension()>
        Public Function LinkToUnlessCurrent(ByVal helper As HtmlHelper,
                                        ByVal linkText As String,
                                        ByVal action As String,
                                        ByVal controller As String,
                                        Optional ByVal other As Object = Nothing,
                                        Optional ByVal htmlAtts As Object = Nothing
                                                                            ) As String

            Dim currentAction As String
            Dim currentController As String
            currentAction = helper.ViewContext.RouteData.Values("action").ToString()
            currentController = helper.ViewContext.RouteData.Values("controller").ToString()

            If currentController = controller And currentAction = action Then
                Return "" & linkText & ""
            Else
                Return helper.ActionLink(linkText, action, controller, other, htmlAtts).ToString
            End If

        End Function

        <Extension()>
        Public Function LinkToIfInRole(ByVal helper As HtmlHelper,
                                        ByVal roleName As String,
                                        ByVal linkText As String,
                                        ByVal action As String,
                                        Optional ByVal controller As String = Nothing,
                                        Optional ByVal other As Object = Nothing,
                                        Optional ByVal htmlAtts As Object = Nothing) As String

            ' See where we are, default the controller if needed
            Dim currentController As String = helper.ViewContext.RouteData.Values("controller").ToString()
            If controller Is Nothing Then controller = currentController

            If helper.ViewContext.HttpContext.User.IsInRole(roleName) Then
                Return helper.ActionLink(linkText, action, controller, other, htmlAtts).ToString
            Else
                Return String.Empty
            End If

        End Function

Posted Thursday, February 10th, 2011 under ASP.NET MVC.

Leave a Reply