As anticipated here - and also by popular demand - we're publishing an Extension Method sample that will enhance the Html.ActionLink helper method in any Razor View allowing the developer to specify a CultureInfo object - or even a null one, to use the CultureInfo.CurrentCulture - that will be used to build a multi-language ActionLink (i.e. an ActionLink with a multi-language URL). Needless to say, such URL will only work if we configured a multi-language aware Route within our application such as the one we mentioned in this post.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
using Ryadel.Components.Util; using System; using System.Globalization; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Html; namespace Ryadel.Web.Mvc.Extensions { public static class HtmlHelperExtensions { /// <summary> /// Extension method to handle localized URL using a dedicated, multi-language custom route. /// for additional info, read the following post: /// https://www.ryadel.com/en/setup-a-multi-language-website-using-asp-net-mvc/ /// </summary> public static IHtmlString ActionLink( this HtmlHelper helper, string linkText, string actionName, string controllerName, object routeValues, string htmlAttributes, CultureInfo cultureInfo) { // fallback if cultureInfo is NULL if (cultureInfo == null) cultureInfo = CultureInfo.CurrentCulture; // arrange a "localized" controllerName to be handled with a dedicated localization-aware route. string localizedControllerName = String.Format("{0}/{1}", cultureInfo.TwoLetterISOLanguageName, controllerName); // build the ActionLink return helper.ActionLink( linkText, actionName, localizedControllerName, routeValues, htmlAttributes); } } } |
Paste this code within a HtmlHelperExtensions.cs file and place it in a suitable folder within your application such as /AppCode/ . In order to use it within our Razor Views we also have to include it in the web.config file placed in the /Views/ folder:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<system.web.webPages.razor> <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <pages pageBaseType="System.Web.Mvc.WebViewPage"> <namespaces> <add namespace="System.Linq" /> <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.Globalization" /> <add namespace="Ryadel.Web.Mvc.Extensions" /> </namespaces> </pages> </system.web.webPages.razor> |
In our sample we used the Ryadel.Web.Mvc.Extensions namespace, but you can change that name accordingly to your needs.
Once done, we'll be able to use the extension method from any of our Razor Views in the following way:
1 |
@Html.ActionLink("linkText", "actionName", "Home", null, null, (CultureInfo)null) |
In the above example we intentionally passed a null CultureInfo reference because we know that the default CurrentInfo.CurrentCulture instance will be used instead. If we followed our multi-language guide, that will do the job.
If you want to take the chance to create a similar extension method for the Url.Action method too, we strongly suggest reading this post.
2 Comments on “Html.ActionLink Extension Method written in C# to handle multi-language Routes in ASP.NET MVC”