2016-05-21 15 views
2

Ich habe eine ASP.NET-Website. Es ist mir gelungen, eingebettete Ansichten von einem anderen Projekt zu laden, aber ich kann keine Lokalisierung für die Ausführung erhalten. Es funktioniert gut, mit Blick aus dem Start Montage aber wenn die embed diejenigen Laden gibt es eine Fehlermeldung:Ansichtslokalisierung in ASP.NET Core einbetten

InvalidOperationException: Kein Service für Typ ‚Microsoft.AspNetCore.Mvc.Localization.IHtmlLocalizer‘ registriert wurde.

Der Code für die Ansicht ist, wie folgend:

_ViewImports.cshtml (einbetten):

@using Microsoft.AspNetCore.Mvc.Localization 
@inject IHtmlLocalizer Localizer 
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers 

Index.cshtml (einbetten)

@{ 
    ViewData["Title"] = Localizer["Home"]; 
} 

<div> 
    @Localizer["Test"] 
</div> 

Mein startup.cs (im Hauptprojekt) sieht so aus:

namespace My.Name.Space //Not the real one 
{ 
    public class Startup 
    { 
     public Startup(IHostingEnvironment env) 
     { 
      //Standard builder function 
     } 

     public IConfigurationRoot Configuration { get; set; } 

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

      //Add EF DbContext, identity, etc. 

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

      //Localization 

      var supportedCultures = GetListOfCultures(); 

      services.Configure<RequestLocalizationOptions>(options => 
      { 
       options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US"); 
       options.SupportedCultures = supportedCultures; 
       options.SupportedUICultures = supportedCultures; 
      }); 
     } 

     public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
     { 
      var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>(); 
      app.UseRequestLocalization(locOptions.Value); 

      loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
      loggerFactory.AddDebug(); 

      if (env.IsDevelopment()) 
      { 
       app.UseBrowserLink(); 
       app.UseDeveloperExceptionPage(); 
       app.UseDatabaseErrorPage(); 
      } 
      else 
      { 
       app.UseExceptionHandler("/Home/Error"); 

       // For more details on creating database during deployment see http://go.microsoft.com/fwlink/?LinkID=615859 
       try 
       { 
        using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>() 
         .CreateScope()) 
        { 
         serviceScope.ServiceProvider.GetService<ApplicationDbContext>() 
          .Database.Migrate(); 
        } 
       } 
       catch { } 
      } 

      app.UseStaticFiles(); 

      app.UseIdentity(); 

      // To configure external authentication please see http://go.microsoft.com/fwlink/?LinkID=532715 

      app.UseMvc(routes => 
      { 
       // Areas support 
       routes.MapRoute(
        name: "areaRoute", 
        template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"); 

       routes.MapRoute(
        name: "default", 
        template: "{controller=Home}/{action=Index}/{id?}"); 
      }); 
     } 
    } 
} 

Fehle ich etwas oder ist es ein Fehler in RC2?

Grüße,

Antwort

0

Ich habe genau das gleiche Problem mit meiner Anwendung ein Upgrade von RC1 zu RC2.

Ich änderte

services.AddLocalization(options => options.ResourcesPath = "Resources"); 

zu

services.AddLocalization(); 

Sie benötigen hierfür Ihre * RESX-Dateien in den Ordner Ressourcen Ihres Web-Projekt arbeiten müssen.

0

Stellt sich heraus, ich legte

@inject IHtmlLocalizer Localizer 

statt

@inject IViewLocalizer Localizer 

Danke,