4

私は多言語ウェブサイトを持つためにASP.Coreを試しています。だから、私は私のStartUp.csを持っている:私は私の_ViewImports.csで'Microsoft.AspNetCore.Mvc.Localization.IHtmlLocalizer'タイプのサービスは登録されていません

public void ConfigureServices(IServiceCollection services) 
{ 
    services.AddLocalization(); 
    services.Configure<RequestLocalizationOptions>(
    opts => 
    { 
     var supportedCultures = new[] 
     { 
      new CultureInfo("de-DE"), 
      new CultureInfo("de"), 
      new CultureInfo("fr-FR"), 
      new CultureInfo("fr"), 
     }; 
     opts.DefaultRequestCulture = new RequestCulture("fr-FR"); 
     // Formatting numbers, dates, etc. 
     opts.SupportedCultures = supportedCultures; 
     // UI strings that we have localized. 
     opts.SupportedUICultures = supportedCultures; 
    }); 
    // Add framework services. 
    services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); 
    services.AddIdentity<ApplicationUser, IdentityRole>() 
     .AddEntityFrameworkStores<ApplicationDbContext>() 
     .AddDefaultTokenProviders(); 
    services.AddMvc(); 
    // Add application services. 
    services.AddTransient<IEmailSender, AuthMessageSender>(); 
    services.AddTransient<ISmsSender, AuthMessageSender>(); 
} 

@using System.Threading.Tasks 
@using Microsoft.AspNetCore.Builder 
@using Microsoft.AspNetCore.Localization 
@using Microsoft.AspNetCore.Mvc.Localization 
@using Microsoft.Extensions.Options 

@inject IHtmlLocalizer Localizer 
@inject IOptions<RequestLocalizationOptions> LocOptions 
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers 

エラー:

An unhandled exception occurred while processing the request. 

InvalidOperationException: No service for type 'Microsoft.AspNetCore.Mvc.Localization.IHtmlLocalizer' has been registered. 

答えて

5

はIHtmlLocalizer like the docs demonstrate.

にタイプを追加します
@inject IHtmlLocalizer<MyType> MyTypeLocalizer 

また、あなたはViewLocalizationサービスを登録していません。あなたもそれをする必要があるかもしれません。

public void ConfigureServices(IServiceCollection services) 
{ 
    services 
     .AddLocalization(options => options.ResourcesPath = "Resources"); 

    services 
     .AddMvc() 
     .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix; 

    ... 
+2

Thanks、services.AddMvc()。AddViewLocalization()が役に立ちます。 – Rroman

+0

AddMvcLocalization()は、MvcLocalizationServices.AddMvcLocalizationServicesを使用します。 register IHtmlLocalizer <>およびIViewLocalizerはどちらですか? – Rroman

関連する問題