The most interesting way I found to solve the problem is by creating my own extensions for the HtmlHelper
. For your case, that you want to use the Fontawesome in the link, the code below solves:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MeuProjeto.Extensions
{
public static class HtmlHelperExtensions
{
public static IHtmlString FontAwesomeActionLink(this HtmlHelper htmlHelper, string linkText,
string actionName, string fontAwesomeClass, object routeValues = null, object htmlAttributes = null)
{
return FontAwesomeActionLink(htmlHelper, linkText, actionName, null, fontAwesomeClass, routeValues,
htmlAttributes);
}
public static IHtmlString FontAwesomeActionLink(this HtmlHelper htmlHelper, string linkText,
string actionName, string controllerName, string fontAwesomeClass, object routeValues = null, object htmlAttributes = null)
{
var targetUrl = UrlHelper.GenerateUrl(null, actionName, controllerName, ObjectToDictionary(routeValues), htmlHelper.RouteCollection, htmlHelper.ViewContext.RequestContext, true);
return new MvcHtmlString(GenerateLink(linkText, targetUrl, fontAwesomeClass, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)));
}
private static string GenerateLink(string linkText,
string targetUrl,
string fontAwesomeClass,
IDictionary<string, object> htmlAttributes
)
{
var a = new TagBuilder("a");
a.MergeAttributes(htmlAttributes);
a.MergeAttribute("href", targetUrl);
var i = new TagBuilder("i");
i.MergeAttribute("class", "fa " + fontAwesomeClass);
a.InnerHtml = i.ToString(TagRenderMode.Normal) + " " + linkText;
return a.ToString(TagRenderMode.Normal);
}
}
}
Register the extension on web.config
inside the directory Views
:
<configuration>
...
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization" />
<add namespace="System.Web.Routing" />
<add namespace="System" />
<add namespace="MeuProjeto.Extensions" /> <!-- Este -->
</namespaces>
</pages>
</system.web.webPages.razor>
...
</configuration>
Use:
@Html.FontAwesomeActionLink("Classificação Financeira", "Indice", "ClassificacaoFinanceira", "fa fa-bar-chart")
This would not help you: <a href="@Url.Action("Index", "Classificacaofinanceira")"><i class="fa fa-bar-Chart"></i></a>
– Janderson Thomaz