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

5 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
  3. Hi, I have created the UrlHelperExtension class and used Url.Action in my controller but it is not using the UrlHelperExtension. I am using ASP.net Core 2.0 and I have other classes in the name space which it is calling but not for Url.Action.

    1. That code was written for .NET Framework 4.x, not for ASP.NET Core. That said, I would try to replace that

      this UrlHelper helper

      with

      this Microsoft.AspNetCore.Mvc.IUrlHelper helper

      to be sure that you’re using the proper UrlHelper class.

      Let me know if it works.

      1. This doesn’t work , changing to “this Microsoft.AspNetCore.Mvc.IUrlHelper helper” doesnt make a different.
        It says Simplify name by removing “Microsoft.AspNetCore.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.