I am trying to localize my app's URLs. Unfortunately, most of the pages show me examples of app localization Like :
This is not what I want. I would to localize the URLs like that:
http://localhost/Bienvenue [ welcome word in French ]
The culture has aleady been managed on my side with a cookie and working well with "CookieRequestCultureProvider" class.
So I have this information and localization in pages are OK.
I succeeded to register all the routes I need. Both of example above working and display the page. Thanks to this :
public void Apply(ApplicationModel application)
{
foreach (var controller in application.Controllers)
{
foreach (var action in controller.Actions)
{
var localizedRouteAttributes = action.Attributes.OfType<LocalizedRouteAttribute>().ToArray();
if (localizedRouteAttributes.Any())
{
foreach (var localizedRouteAttribute in localizedRouteAttributes)
{
var localizedVersions = GetLocalizedVersionsForARoute(localizedRouteAttribute.Name); // GetLocalizedVersionsForARoute contains all routes translated and group by culture.
foreach (var localizedVersion in localizedVersions)
{
if (!action.Selectors.Any(s => s.AttributeRouteModel.Template == localizedVersion.Template))
action.Selectors.Add(new SelectorModel(action.Selectors.First()) { AttributeRouteModel = localizedVersion });
}
}
}
}
}
}
So mvc take the last route register in Selectors (if FR, it take FR route). I can't manage the other routes by this piece of code because it's load with the app. And can't work with a dynamic use (The app permit to change the lang when I want).
Thanks in advance.
I found this example project works: https://github.com/tomasjurasek/AspNetCore.Mvc.Routing.Localization
Once it's set up, you can tag routes with
[LocalizedRoute("culture", "RouteName")]
Do that for each culture you want a unique name for, and the dynamic route it creates will translate to the proper action and execute it. It's also got a tag helper for creating translated links, though if you want to use Url.Action or Html.ActionLink, I find you have to create extension methods that take the culture into account to get them to work fully.
In your case wanting them at the route level instead of /culture/Controller/Action may take some more work, but it might be a useful starting place for you.
look in this little example I hope to help you :)
1) in your controller :
[RoutePrefix("/")]
public HomeController : Controller {
[HttpGet]
[Route("Welcome")]
public ActionResult Index() {
return View();
}
}
And enable it in route table " routes.MapMvcAttributeRoutes(); " like this
public class RouteConfig {
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//enable attribute routing
routes.MapMvcAttributeRoutes();
//convention-based routes
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = "" }
);
}
}
I suggest reading this article from this URL: https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/
if you have any other question you can ask me
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.