在ASP MVC 3項目中,我希望啓用語言切換。使用ActionLink的ASP MVC 3切換語言
路由的定義是這樣的:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"DefaultWithLanguage", // Route name
"{language}/{controller}/{id}/{slug}", // URL with parameters
new { language = "en", controller = "Front", action = "Details", id = UrlParameter.Optional, slug = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
嘗試切換語言(在_Layout.cshtml)的工作原理是這樣的:
而是得到一個URL像下面的(具有後<li>@Html.ActionLink("Spanish", ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), new { language = "es" })</li>
選定的西班牙語)
.../es/ControllerName/ActionName
我得到這個:
.../ControllerName/ActionName?Length=11
如果我設置了ActionLink的以下(注意最後一個空參數):
<li>@Html.ActionLink("Spanish", ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), new { language = "es" }, null)</li>
我得到這個:
.../ControllerName/ActionName?language=es
我在想什麼? 在此先感謝!
您是否也離開了默認路線?請顯示您的所有路線註冊。 – 2012-02-19 09:35:58
@Darin看看(改變的)公式。我已將所有代碼包含在RegisterRoutes中。 – 2012-02-19 09:53:00