[英]ASP.NET Core API System.InvalidOperationException in localization
i've been trying to use localization in my webAPI but everytime i have this error我一直在尝试在我的 webAPI 中使用本地化,但每次出现此错误
System.InvalidOperationException: No service for type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory' has been registered.
System.InvalidOperationException:没有注册类型为“Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory”的服务。
here's my Program.cs这是我的 Program.cs
using Microsoft.OpenApi.Models;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Localization;
using System.Globalization;
using testeTraducao.Controllers;
using Microsoft.Extensions.Options;
using testeTraducao.Localize;
using testeTraducao.Resources;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddLocalization(opt => opt.ResourcesPath = "Resources");
builder.Services.AddControllers();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new() { Title = "testeTraducao", Version = "v1" });
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "testeTraducao v1"));
}
app.UseHttpsRedirection();
var supportedCultures = new[]
{
new CultureInfo("en-UK"),
new CultureInfo("pt-BR")
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-UK"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
});
app.UseAuthorization();
app.MapControllers();
app.Run();
i saw some people saying to add builder.Services.AddMvc();
我看到有人说要添加
builder.Services.AddMvc();
but, still not working, but with a different error但是,仍然无法正常工作,但出现了不同的错误
System.InvalidOperationException: The view 'test' was not found. The following locations were searched:
/Views/Traduzido/test.cshtml
/Views/Shared/test.cshtml
/Pages/Shared/test.cshtml
at Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult.EnsureSuccessful(IEnumerable`1 originalLocations)
at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)
at Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|30_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
here's some infos:这里有一些信息:
my folders structure: Folder structure我的文件夹结构:文件夹结构
Controller: Controller:
public class TraduzidoController : Controller
{
private readonly IStringLocalizer<SharedResources> _localizador;
public TraduzidoController(IStringLocalizer<SharedResources> localizador)
{
_localizador = localizador;
}
[HttpGet("/traduz")]
public IActionResult Index()
{
return View(_localizador["test"]);
}
}
target framework: <TargetFramework.net6.0</TargetFramework>
目标框架:
<TargetFramework.net6.0</TargetFramework>
thanks谢谢
--edit-- after changing to return Ok(_localizador["test"]);
--edit-- 更改为
return Ok(_localizador["test"]);
still cant get the message check the return here仍然无法收到消息,请在此处检查返回
return View(_localizador["test"]);
means that you are attempting to get a view called test
, which in this case is localised.意味着您正在尝试获取一个名为
test
的视图,在本例中它是本地化的。
Definitely add builder.Services.AddMvc();
绝对添加
builder.Services.AddMvc();
but then also add in a view file, which is what the errors are detailing:但随后还添加了一个视图文件,这是错误的详细说明:
The view 'test' was not found
Create a root Views
folder, a Traduzido
folder inside there for your controller's views & finally, add in a test.cshtml
file to fix the errors.创建一个根
Views
文件夹,里面有一个Traduzido
文件夹用于您的控制器的视图,最后,添加一个test.cshtml
文件来修复错误。
try adding builder.Services.AddControllersWithViews();
尝试添加
builder.Services.AddControllersWithViews();
to your program.cs file.到您的 program.cs 文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.