Html.ActionLink Extension Method written in C# to handle multi-language Routes in ASP.NET MVC

Custom Routing and Action Method Names in ASP.NET 5 and ASP.NET MVC 6

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.

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:

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:

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.

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

2 Comments on “Html.ActionLink Extension Method written in C# to handle multi-language Routes in ASP.NET MVC”

  1. Pingback: Setup a multi-language website using ASP.NET MVC
  2. Pingback: Url.Action Extension Method for multi-language Routes in NET MVC

Leave a Reply

Your email address will not be published. Required fields are marked *

The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.