我的数据库上有一个Language
表。有一种主要语言。我的分层架构软件。层:域-数据(存储库(-业务-呈现 我正在使用 Ninject 进行 DI。
因此,我应该在应用程序启动时获取主要语言并设置我的网址,例如:{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 }
);
}
}
我解决了这个问题。路由配置文件应该像这样才能获得主要语言。
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 }
);
}
}