公司效益好了,準備和國外做生意,這個時候就需要多語言了。
> 1. 這是一個ASP.NET Core Web多語言項目,主要展示項目的不同:
> 2. 第一種:www.xxx.com/en/index; www.xxx.com/zh/index; ,這種事通過路由來處理的
> 3. 第二種: www.xxx.com/index/en; www.xxx.com/index/zh ,這種可以用cookie保存,通過cookie來判斷
> 4. 第三種:www.en.xxx.com; www.zh.xxx.com,這種方案就是發布兩個項目,屬于比較簡單的,有多少種語言就發布多少種,上面的兩種發布的是同一個版本的
> 5. 第一種,第三種一般用于兩個項目差別比較大的情況,第二種一般用于項目只有內容不同,其他的都相同的情況
創建項目
新建一個ASP.NET Core Web項目,選擇Web應用程序(模型視圖控制器)
創建controller
創建一個EnController、ZhController、MoreController
using Microsoft.AspNetCore.Mvc;namespace MoreLanguage.Controllers {public class HomeController : Controller{public IActionResult Index(){return View();}} }
using Microsoft.AspNetCore.Mvc;namespace MoreLanguage.Controllers {public class EnController : Controller{public ActionResult Index(){return View();}} }
using Microsoft.AspNetCore.Mvc;namespace MoreLanguage.Controllers {public class ZhController : Controller{public ActionResult Index(){return View();}} }
using Microsoft.AspNetCore.Mvc;namespace MoreLanguage.Controllers {public class MoreController : Controller{public ActionResult Index(string lang){ViewBag.lang = lang == "en" ? "Hello World!" :lang == "zh" ? "世界,你好!" : $"你的語言我不懂:{lang}";return View();}} }
創建對應的視圖
@{ViewData["Title"] = "Index"; }<h2>Hello World!</h2>
這里不再重復視圖代碼
修改Route
//Startup.cs文件 app.UseMvc(routes => {routes.MapRoute(name: "more_route",template: "More/{action}/{lang}",defaults: new { controller = "More" });routes.MapRoute(name: "default",template: "{controller=Home}/{action=Index}/{id?}"); });
效果
net開源開發web框架?
?
不同的路由查看效果
https://localhost:44318/
https://localhost:44318/En
?https://localhost:44318/Zh
https://localhost:44318/More/Index/en
ASPNET程序開發招聘。https://localhost:44318/More/Index/zh
git地址
https://github.com/jasonhua95/samll-project/tree/master/MoreLanguage