How to route a multiple language URL with a MVC

后端 未结 4 1374
执念已碎
执念已碎 2020-12-08 15:53

I need multi-language URL route of existing controller. Let me explain more:

I have a controller with name \"Product\" and View with name \"Software\"; therefore, by

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-08 16:08

    As has been suggested before, this does depart from convention where the website urls (and routes) use English.

    Nevertheless, it is possible, but in order to do it, you'll probably have to look at generating one route per action for every foreign language. So for a website with 20 actions and three languages (English, French, and German), you'll need 41 routes (20 French, 20 German and 1 English). Not the most efficient system, I admit, but it works as you want it to.

    //You'll only need one of these, which is the default.
    routes.MapRoute(
      "English route",
      "en/{controller}/{action}/{id}"
      new { controller = "Home", action = "Index", language = "en" },
    );
    
    routes.MapRoute(
      "FrenchHome",
      "fr/Demarrer/Index/{id}",
      new { controller = "Home", action = "Index", language = "fr" }
    );
    
    routes.MapRoute(
      "GermanHome",
      "de/Heim/Index/{id}", //'Heim' is, I believe the correct usage of Home in German.
      new { controller = "Home", action = "Index", language = "de" }
    );
    
    //Some more routes...
    
    routes.MapRoute(
      "FrenchSoftware",
      "fr/Produit/Logiciels/{id}",
      new { controller = "Product", action = "Software", language = "fr" }
    );
    
    routes.MapRoute(
      "GermanSoftware",
      "de/Produkt/Software/{id}", //In this instance, Software should be the same in German and English.
      new { controller = "Product", action = "Software", language = "de" }
    );
    
    //And finally, the 404 action.
    routes.MapRoute(
      "Catchall",
      "{language}/{*catchall}",
      new { controller = "Home", action = "PageNotFound", language = "en" },
      new { language = "^(en|fr|de)$" }
    );
    
    //This is for the folks who didn't put a language in their url.
    routes.MapRoute(
      "Catchall",
      "{*catchall}",
      new { controller = "Home", action = "PageNotFound", language = "en" }
    );
    

    In your actions, for example Product/Software...

    public ActionResult Software(string language, int id)
    {
      //This would go off to the DAL and get the content in whatever language you want.
      ProductModel model = ProductService.GetSoftware(language, id);
    
      return View(model);
    }
    

    I would LOVE it if somebody came along and said that there's a better way of doing this, because I agree that having the url in a foreign language isn't good, and given that the Internet itself is moving towards allowing non-Roman characters in urls, the sooner we look at solutions to this, the better.

    Not only that, but I know proud French people don't like to see their website urls contain English. :)

提交回复
热议问题