我有一个自定义约定,需要在其中包含本地化字符串。据我所知,实例化 IStringLocalizer 的唯一方法是 DependencyInjection。
如何在 CustomConvention 中使用 IStringLocalizer?
约定是这样注册的
public void ConfigureServices(IServiceCollection services)
{
//First I register the localization settings
services.AddLocalization(o =>
{
o.ResourcesPath = "Resources";
});
services.AddMvc(options =>
{
//My custom convention, I would need to inject an IStringLocalizer
//into constructor here, but I can' instantiate it
options.Conventions.Add(new LocalizationRouteConvention());
})
}
最佳答案
我的解决方案并不完美。
您可以做类似的事情,构建服务提供者以获取 StringLocalizer 的实例。
public void ConfigureServices(IServiceCollection services)
{
//First I register the localization settings
services.AddLocalization(o =>
{
o.ResourcesPath = "Resources";
});
var stringLocalizer = services.BuildServiceProvider().GetService<IStringLocalizer<Resource>>();
services.AddMvc(options =>
{
//My custom convention, I would need to inject an IStringLocalizer
//into constructor here, but I can' instantiate it
options.Conventions.Add(new LocalizationRouteConvention(stringLocalizer));
})
}
关于asp.net-mvc - 如何将 IStringLocalizer 注入(inject) IApplicationModelConvention?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53869945/