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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
using System; using System.Globalization; using System.Web.Mvc; namespace Ryadel.Web.Mvc.Extensions { public static class UrlHelperExtensions { /// <summary> /// Extension method to handle localized URL using a dedicated, multi-language custom route. /// for additional info, read the following post: /// https://www.ryadel.com/en/setup-a-multi-language-website-using-asp-net-mvc/ /// </summary> public static string Action( this UrlHelper helper, string actionName, string controllerName, object routeValues, CultureInfo cultureInfo) { // fallback if cultureInfo is NULL if (cultureInfo == null) cultureInfo = CultureInfo.CurrentCulture; // arrange a "localized" controllerName to be handled with a dedicated localization-aware route. string localizedControllerName = String.Format("{0}/{1}", cultureInfo.TwoLetterISOLanguageName, controllerName); // build the Action return helper.Action( actionName, localizedControllerName, routeValues); } } } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<system.web.webPages.razor> <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <pages pageBaseType="System.Web.Mvc.WebViewPage"> <namespaces> <add namespace="System.Linq" /> <add namespace="System.Web.Mvc" /> <add namespace="System.Web.Mvc.Ajax" /> <add namespace="System.Web.Mvc.Html" /> <add namespace="System.Web.Optimization"/> <add namespace="System.Web.Routing" /> <add namespace="System.Globalization" /> <add namespace="Ryadel.Web.Mvc.Extensions" /> </namespaces> </pages> </system.web.webPages.razor> |
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:
1 |
@Url.Action("actionName", "Home", null, (CultureInfo)null) |
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.
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.
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.
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”