对于多语言ASP.NET MVC 3 Web应用程序,我正在确定Thread.CurrentThread.CurrentCulture和Thread.CurrentThread.CurrentUICulture控制器工厂,如下所示:
public class MyControllerFactory : DefaultControllerFactory {
protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType) {
//Get the {language} parameter in the RouteData
string UILanguage;
if (requestContext.RouteData.Values["language"] == null)
UILanguage = "tr";
else
UILanguage = requestContext.RouteData.Values["language"].ToString();
//Get the culture info of the language code
CultureInfo culture = CultureInfo.CreateSpecificCulture(UILanguage);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
return base.GetControllerInstance(requestContext, controllerType);
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码已经快一年了!所以,我愿意接受建议.
我在Global.asax文件上注册这个,如:
ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());
Run Code Online (Sandbox Code Playgroud)
这样做很好,但我不确定这是否是做这种行为的最佳做法和最佳场所.
我没有挖掘主要角色,ControllerFactory我无法将其与之比较ActionFilterAttribute.
你怎么看待这种行动的最佳地点?
我一直在尝试使用自定义实现FXMLLoader的setControllerFactory方法Callback<P,R>.
ORACLE文档说明如下:
实现可能返回null值以指示它不能或不能创建给定类型的控制器; 在这种情况下,加载器将使用默认控制器构造机制.
我想要实现的结果是我可以使用依赖注入框架来创建任何需要参数的控制器,但我会让FXMLLoader任何不需要参数的控制器加载.
所以,如果我有以下简单的FXML文件,它使用ViewController不接受参数的类...
<StackPane fx:id="pane"
xmlns:fx="http://javafx.com/fxml"
fx:controller="my.package.ViewController">
</StackPane>
Run Code Online (Sandbox Code Playgroud)
并且我使用以下简单的控制器工厂实现来发信号通知FXMLLoader我希望它在这种情况下管理控制器的构造...
loader.setControllerFactory(new Callback<Class<?>, Object>(){
@Override
public Object Call(Class<?> type) {
return null; // Let the FXMLLoader handle construction...
}
});
Run Code Online (Sandbox Code Playgroud)
在调用load()方法之后,我的ViewController类中的Initialise方法永远不会被调用(我已用断点验证了这一点).
如果我更改我的控制器工厂实现以返回ViewController该类的实例,那么一切都按预期工作.
任何人都可以帮我解决我的困惑吗?我是否Callback错误地使用了界面或者ORACLE文档不正确?
我的主要ASP.NET MVC组合应用程序使用全局Unity容器来注册类型.该应用程序将控制器工厂设置为使用此全局容器.我想重构这一点,以便我的每个可移植区域都利用它自己的子Unity容器,以便不同的区域可以以不同的方式实现接口.看起来每个区域需要一个不同的ControllerFactory.我将如何实现这一目标,因为以下内容为所有人设置工厂?
ControllerBuilder.Current
.SetControllerFactory(/* controller factory with global unity container */);
Run Code Online (Sandbox Code Playgroud) asp.net-mvc dependency-injection unity-container controller-factory asp.net-mvc-2
我发现了为什么占位符“ Home Controller”在这里被调用
我在RouteConfig.cs中注释掉了对HomeController的引用,并添加了一个我希望它调用的Controller:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//If get 404 error re: favicon, see http://docs.castleproject.org/(X(1)S(vv4c5o450lhlzb45p5wzrq45))/Windsor.Windsor-tutorial-part-two-plugging-Windsor-in.ashx or just uncomment the line below:
//routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
//routes.MapRoute(
// name: "Default",
// url: "{controller}/{action}/{id}",
// defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
//);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "DepartmentsController", action = "GetCountOfDepartmentRecords", id = UrlParameter.Optional }
);
} …Run Code Online (Sandbox Code Playgroud) castle-windsor routes repository controller-factory asp.net-web-api-routing