Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
I am working for an MVC application(razor) with multilanguage, language(combo box) will be selected by the user at the time of login, once login was successfull i need the user to be routed based on the language selected

for eg www.test.com/fr/Home

also suggest me how to route from one controller to another
Posted

C#
public static void RegisterRoutes(RouteCollection routes)
{
    // This one gives you the language
    // Responds to the following url:
    // http://example.com/fr/
    // http://example.com/fr/home/
    routes.MapRoute("Lang",
        "{lang}/{controller}/{action}/{id}",
            new {
                controller = "Home",
                action = "index",
                lang = "fr",
                id = UrlParameter.Optional
            });
    // you can default language here or something just in case
    // user types the following url: http://example.com/
    routes.MapRoute("Default",
        "{controller}/{action}/{id}",
            new {
                controller = "Home",
                action = "index",
                id = UrlParameter.Optional
            });

}

public class HomeController:Controller{
    public ActionResult Index(){
        string language = "fr"; // default language
        object olang = RouteData.Values["lang"];
        if(lang != null){
            // it's possible that lang may be null when user goes to
            // http://example.com/
            language = lang.ToString();
        }
        // Do something now with language... like setting it to the thread
        // Thread.CurrentThread.CurrentCulture = .....;
        
    }

    // Rerouting example
    // http://example.com/home/rerouting -> http://example.com/fr/home/index
    public ActionResult Rerouting(){
        // Redirects to index using french language
        return RedirectToAction("index","home", new { lang = "fr" });
    }
}
 
Share this answer
 
Hi @jlopez788

It worked, thanks for your reply.

Regards,
Sundarsureshin
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900