0

我的数据库上有一个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 }
            );
        }
    }
4

1 回答 1

0

我解决了这个问题。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 }
            );
        }
    }
于 2020-06-09T12:28:10.313 回答