Asp.Net 具有可选语言 URL 段的 MVC MapRoute
Asp.Net MVC MapRoute with optional language URL segment
我有一个具有以下路由映射的 ASP.Net MVC 应用程序:
context.MapRoute("Empty","", new { controller = "Home", action = "Index" });
context.MapRoute("Info","/Info", new { controller = "Info", action = "Index" });
context.MapRoute("Base","/Info/Base", new { controller = "Info", action = "Base" });
我需要将语言前缀添加到 URL 作为段,以便 URL 看起来像这样:
www.something.com/en
www.something.com/en/Info
www.something.com/en/Info/Base
我通过将 languageCode 参数添加到 URL:
轻松实现它
context.MapRoute("Empty","/{languageCode}", new { controller = "Home", action = "Index" });
context.MapRoute("Info","/{languageCode}/Info", new { controller = "Info", action = "Index" });
context.MapRoute("Base","/{languageCode}/Info/Base", new { controller = "Info", action = "Base" });
不幸的是,这个参数应该是可选的。但是当我在那些路线下的 URL 中错过它时 - 我有 404 错误。
有什么想法可以实现吗?添加 languageCode = UrlParameter.Optional 没有帮助,它仅在可选参数尾随 URL.
时有效
添加两个路由配置(带和不带 languageCode
),您将获得所需的行为
context.MapRoute("Empty","/{languageCode}", new { controller = "Home", action = "Index" });
context.MapRoute("Info","/{languageCode}/Info", new { controller = "Info", action = "Index" });
context.MapRoute("Base","/{languageCode}/Info/Base", new { controller = "Info", action = "Base" });
context.MapRoute("Empty","", new { controller = "Home", action = "Index" });
context.MapRoute("Info","/Info", new { controller = "Info", action = "Index" });
context.MapRoute("Base","/Info/Base", new { controller = "Info", action = "Base" });
备注
以下配置与您的相同,但包含的配置代码较少(但它也会暴露所有其他控制器)
routes.MapRoute(
name: "LanguageCode",
url: "{languageCode}/{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index" }
);
我有一个具有以下路由映射的 ASP.Net MVC 应用程序:
context.MapRoute("Empty","", new { controller = "Home", action = "Index" });
context.MapRoute("Info","/Info", new { controller = "Info", action = "Index" });
context.MapRoute("Base","/Info/Base", new { controller = "Info", action = "Base" });
我需要将语言前缀添加到 URL 作为段,以便 URL 看起来像这样:
www.something.com/en
www.something.com/en/Info
www.something.com/en/Info/Base
我通过将 languageCode 参数添加到 URL:
轻松实现它context.MapRoute("Empty","/{languageCode}", new { controller = "Home", action = "Index" });
context.MapRoute("Info","/{languageCode}/Info", new { controller = "Info", action = "Index" });
context.MapRoute("Base","/{languageCode}/Info/Base", new { controller = "Info", action = "Base" });
不幸的是,这个参数应该是可选的。但是当我在那些路线下的 URL 中错过它时 - 我有 404 错误。
有什么想法可以实现吗?添加 languageCode = UrlParameter.Optional 没有帮助,它仅在可选参数尾随 URL.
时有效添加两个路由配置(带和不带 languageCode
),您将获得所需的行为
context.MapRoute("Empty","/{languageCode}", new { controller = "Home", action = "Index" });
context.MapRoute("Info","/{languageCode}/Info", new { controller = "Info", action = "Index" });
context.MapRoute("Base","/{languageCode}/Info/Base", new { controller = "Info", action = "Base" });
context.MapRoute("Empty","", new { controller = "Home", action = "Index" });
context.MapRoute("Info","/Info", new { controller = "Info", action = "Index" });
context.MapRoute("Base","/Info/Base", new { controller = "Info", action = "Base" });
备注
以下配置与您的相同,但包含的配置代码较少(但它也会暴露所有其他控制器)
routes.MapRoute(
name: "LanguageCode",
url: "{languageCode}/{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index" }
);