Site icon Ryadel

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.

Exit mobile version