0

I have several url patterns as follows in the Route:

{lang}/package/{packageID}
{lang}/package/{packageID}/Edit/{partNumber}
...

There's a footer in layout.cshtml, the footer provides different culture links for users to change the language of the website. When the user click the link, I hope it can change the language and stay in the current page, so I render the link by Razor like this:

@Html.ActionLink("English", ViewContext.RouteData.Values["Action"], ViewContext.RouteData.Values["Controller"], new { lang = "en-us"}, null)
@Html.ActionLink("Traditional Chinese", ViewContext.RouteData.Values["Action"], ViewContext.RouteData.Values["Controller"], new { lang = "zh-tw"}, null)
@Html.ActionLink("Japanese", ViewContext.RouteData.Values["Action"], ViewContext.RouteData.Values["Controller"], new { lang = "ja-jp"}, null)
...

But there is a problem is that I need to pass other parameters which I mentioned above to the page accordingly to keep users staying in the current page.

How could I achieve in this scenario?

Thanks!

2
  • ASP.NET MVC will automatically reuse route segments from the current request if they occur earlier than the segment for which you are explicitly providing a value. If you could make your {lang} segment the last in the route, what you want would happen automatically. Commented Nov 5, 2018 at 15:08
  • @GSerg Thnaks for your comment, but it doesn't seem like a good way to do this, It will actually work in this scenario. However it would be a constraint on the url pattern, every pattern needs to put the {lang} at the end, and also make assumptions about the order in which users make requests. But this is a hint, if I still cannot find other solutions, I'll try it. Thank you! Commented Nov 5, 2018 at 16:12

2 Answers 2

2

You may create an extension method as an helper for the creation of language-dependent links:

public static class LanguageExtensions
{
    public static RouteValueDictionary ForLang(this RouteValueDictionary dict, string lang)
    {
        var cloned = new RouteValueDictionary(dict);
        cloned["lang"] = lang;
        return cloned;
    }
}

Now you may use it in this way:

@Html.ActionLink("English", ViewContext.RouteData.Values["Action"], ViewContext.RouteData.Values["Controller"], ViewContext.RouteData.ForLang("en-us"), null)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this way is simple and clear, it's close to my needs.
0

You could use return Redirect(HttpContext.Request.UrlReferrer.ToString()); to return to take users back to the page they were at and not have to worry about passing the parameters again.

The flow would be:

  1. User clicks on a link like @Html.ActionLink("English", ViewContext.RouteData.Values["Action"], ViewContext.RouteData.Values["Controller"], new { lang = "en-us"}, null)

  2. You change the language in the controller's action method

  3. Take user back to the page they were on by calling return Redirect(HttpContext.Request.UrlReferrer.ToString()); from the same action method

1 Comment

Hi Bruno, thank you for your answer, I've tried this way before posting this question, it works. But I wonder if there's a way better than redirect, because it needs to take more time to do another navigation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.