-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Injecting IViewLocalizer into Razor Page causing IndexOutOfRangeException. #6694
Comments
Looks like the issue steams from us not setting the using System;
using System.Diagnostics;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
{
public class LocFixPageFactoryProvider : DefaultPageFactoryProvider
{
public LocFixPageFactoryProvider(
IPageActivatorProvider pageActivator,
IModelMetadataProvider metadataProvider,
IUrlHelperFactory urlHelperFactory,
IJsonHelper jsonHelper,
DiagnosticSource diagnosticSource,
HtmlEncoder htmlEncoder,
IModelExpressionProvider modelExpressionProvider)
: base(pageActivator, metadataProvider, urlHelperFactory, jsonHelper, diagnosticSource, htmlEncoder, modelExpressionProvider)
{
}
public override Func<PageContext, ViewContext, object> CreatePageFactory(CompiledPageActionDescriptor actionDescriptor)
{
var result = base.CreatePageFactory(actionDescriptor);
return (pageContext, viewContext) =>
{
viewContext.ExecutingFilePath = actionDescriptor.RelativePath;
return result(pageContext, viewContext);
};
}
}
} And change your Startup to use this service:
@Eilon \ @rynowak would this be a 2.0.1 worthy? The workaround isn't very pleasant. |
Thanks for the workaround. However, I still unable to show the localized string. I have done the following:
For repro project, please see here and checkout to branch |
@Eilon self-assigning this for further investigation. |
@zulfahmi93 I tried your |
@pranavkm sorry my bad. I tried to rerun it and now it's already working. Thanks for your help! |
Thanks @zulfahmi93. We'll use this work item to track the actual fix. |
@Eilon - this is a bug. |
From aspnet/Localization#407
Title
When injecting
IViewLocalizer
using@inject
directive into any Razor Page (not using MVC),System.IndexOutOfRangeException: Index was outside the bounds of the array.
will be thrown.Minimal repro steps
See this repo.
Expected result
The page should load successfully without ant error.
Actual result
The following error was thrown:
The text was updated successfully, but these errors were encountered: