Udemy

Creating Custom Html Helper Extensions in asp.net mvc

Tuesday, September 09, 2014 0 Comments A+ a-

Hi,

Today I will tell about how to write custom html helpers in asp.net mvc. I will show how to create custom html helper for ActionLink, normally we write Html.ActionLink(parameters) and it renders anchor tag in response as html on the page.

I had a requirement where I needed to check that if user is in specific role then link should render otherwise it will not render on page, so it is very akward to write if blocks in every view where I need to hide links on the basis of role so after researching a little, I came to know that we can write custom extensions according to the requirements.

So I wrote a custom helper that checked the role of the user and deciceds that it should render or not.

ActionLink method exisits inside LinkExtension class which is a static class, so we need to create a static class with name LinkExtensions:


public static class LinkExtensions
{
}

Now in it I will write one overload for my custom extension as I will utitlize that, we can create all overloads that are available for ActionLink same way we are creating this one but for this article I will just share one specific overload that I will be using:


namespace TestCustomHelper.Html
{

public static class LinkExtensions
{
public static MvcHtmlString ActionLinkAuthorized(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes, bool showActionLinkAsDisabled)
  {
     if (htmlHelper.ActionAuthorized(actionName, controllerName))
     {
       return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes);
     }
     return MvcHtmlString.Empty;
}

}

Here is the ActionAuthorized method which also an Action Extension, it returns bool value by checking that user is authorized to call this action of controller or not and on the basis of value returned by this method it decides weather render the actionlink or not:


namespace HealthAccessPoint.Extensions
{
    public static class ActionExtensions
    {
        public static bool ActionAuthorized(this HtmlHelper htmlHelper, string actionName, string controllerName)
        {
            ControllerBase controllerBase = string.IsNullOrEmpty(controllerName) ? htmlHelper.ViewContext.Controller : htmlHelper.GetControllerByName(controllerName);
            ControllerContext controllerContext = new ControllerContext(htmlHelper.ViewContext.RequestContext, controllerBase);
            ControllerDescriptor controllerDescriptor = new ReflectedControllerDescriptor(controllerContext.Controller.GetType());
            ActionDescriptor actionDescriptor = controllerDescriptor.FindAction(controllerContext, actionName);

            if (actionDescriptor == null)
                return false;

            FilterInfo filters = new FilterInfo(FilterProviders.Providers.GetFilters(controllerContext, actionDescriptor));

            if (!htmlHelper.CheckAccessRight(actionName, controllerDescriptor.ControllerName))
            {
                return false;
            }

            return true;
        }
    }
}

Now in my View I will add its namespace and then use it:


@using TestCustomHelper.Html


@Html.ActionLinkAuthorized("Create New", "Create", new { org = ViewBag.OrgBranchID }, new { @id = "linkCreateEmployee" },true) 

Cheers! and Happy Coding
Coursera - Hundreds of Specializations and courses in business, computer science, data science, and more