gpt4 book ai didi

c# - 在 ASP.NET Core 中嵌入 View 本地化

转载 作者:太空宇宙 更新时间:2023-11-03 15:21:08 28 4
gpt4 key购买 nike

我有一个 ASP.NET 网站。我成功地从另一个项目加载了嵌入 View ,但我无法运行本地化。它适用于启动程序集的 View ,但在加载嵌入 View 时出现错误消息:

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

View 代码如下:

_ViewImports.cshtml(嵌入):

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

Index.cshtml(嵌入)

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

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

我的 startup.cs(在主项目中)如下所示:

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?}");
});
}
}
}

我是不是遗漏了什么或者是 RC2 中的错误?

问候,

最佳答案

我的应用程序从 RC1 升级到 RC2 时遇到了完全相同的问题。

我变了

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

services.AddLocalization();

您的 Web 项目的 Resources 文件夹中需要有 *.resx 文件才能工作。

关于c# - 在 ASP.NET Core 中嵌入 View 本地化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37368279/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com