'Url.Action and culture in ASP.Net core
On my bilingual site (English & Spanish) developed using ASP.Net Core 3.1, I have 2 distinct views (staff and program) populated by their own controller/viewmodels which contains the exact same links:
<a href='@Url.Action("Detail", "Staff", new { nodeAlias= "JohnDoe" })' class="...">TEST STAFF</a>
<a href='@Url.Action("Detail", "Program", new { nodeAlias="group1"})' class="...">TEST PROGRAM</a>
These links are associated with routes such as the following:
endpoints.MapControllerRoute(
name: "ProgramDetail",
pattern: "{culture}/Research/Program/{nodeAlias}",
defaults: new { action = "Detail", controller = "Program", culture = DefaultCulture, nodeAlias = "default", nodeParent = "Research" },
constraints: new { culture = new SiteCultureConstraint() }
);
endpoints.MapControllerRoute(
name: "AboutStaff",
pattern: "{culture}/About/Staff/{action}/{nodeAlias?}",
defaults: new { action = "Index", controller = "Staff", culture = DefaultCulture, nodeParent = "About" },
constraints: new { culture = new SiteCultureConstraint(), nodeAlias = new AlphaRouteConstraint() }
);
The default culture is English and it works file in this language.
However, I am having issues when I switch to Spanish.
For example, In Spanish, when I am on the program page, the program link is correctly built (eg.:/es/Research/program/group1) while the link is wrong for the staff page (en/About/Staff/Detail/johnDoe).
But if I switch to the staff page which contains the exact same links, i get the wrong culture for the program link (/en/Research/program/group1) but the good one for the staff page (es/About/Staff/Detail/johnDoe).
It looks like the pattern is that links generated by url.action are using the current culture only on the pages from the matching controller/viewmodel/context.
It is quite unclear to me if it is normal and why I have this behaviour. Anyone could explain it to me and how to solve it?
Thank you,
S.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
