如何在 MVC RouteConfig 中设置默认语言?
How to set default language in MVC RouteConfig?
我的数据库中有 Language
table。有一种主要语言。我的分层架构软件。图层:域数据(存储库)-业务演示 我正在使用 Ninject 作为 DI。
所以,我应该在应用程序启动时获取主要语言并将我的 url 设置为:{language}/{controller}/{action}/{id}
我该怎么做?因为我无法访问 RouteConfig.cs
或 Global.asax
上的业务层,有办法吗?
我想这样做:
public class RouteConfig
{
private ILanguageBusiness _languageBusiness;
public RouteConfig(ILanguageBusiness languageBusiness)
{
_languageBusiness = languageBusiness;
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{language}/{controller}/{action}/{id}",
defaults: new { language = _languageBusiness.GetPrimaryLanguageCode(), controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
我解决了这个问题。 RouteConfig 文件应该像这样才能获取主要语言。
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
var languageBusiness = DependencyResolver.Current.GetService<ILanguageBusiness>();
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{language}/{controller}/{action}/{id}",
defaults: new { language = languageBusiness.GetPrimaryLanguage().Code, controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
我的数据库中有 Language
table。有一种主要语言。我的分层架构软件。图层:域数据(存储库)-业务演示 我正在使用 Ninject 作为 DI。
所以,我应该在应用程序启动时获取主要语言并将我的 url 设置为:{language}/{controller}/{action}/{id}
我该怎么做?因为我无法访问 RouteConfig.cs
或 Global.asax
上的业务层,有办法吗?
我想这样做:
public class RouteConfig
{
private ILanguageBusiness _languageBusiness;
public RouteConfig(ILanguageBusiness languageBusiness)
{
_languageBusiness = languageBusiness;
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{language}/{controller}/{action}/{id}",
defaults: new { language = _languageBusiness.GetPrimaryLanguageCode(), controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
我解决了这个问题。 RouteConfig 文件应该像这样才能获取主要语言。
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
var languageBusiness = DependencyResolver.Current.GetService<ILanguageBusiness>();
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{language}/{controller}/{action}/{id}",
defaults: new { language = languageBusiness.GetPrimaryLanguage().Code, controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}