Skip to main content

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

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.

RELATED POSTS

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.

Close
Read previous post:
How to import Wikipedia Templates into your own MediaWiki

If you stumbled upon this post, chances are you just installed your very own MediaWiki and want to make it...

Close