Url.Action 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 Url.Action 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 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 UrlHelperExtensions.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 Html.ActionLink 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

0 Comments on “Url.Action Extension Method written in C# to handle multi-language Routes in ASP.NET MVC”

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

Leave a Reply

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

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