Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Injecting IViewLocalizer into Razor Page causing IndexOutOfRangeExcep…
Browse files Browse the repository at this point in the history
…tion

Fixes #6694
  • Loading branch information
pranavkm authored and ajaybhargavb committed Sep 15, 2017
1 parent 3e8da43 commit c13cef6
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ private object CreateInstance()
_tempDataFactory.GetTempData(_pageContext.HttpContext),
TextWriter.Null,
_htmlHelperOptions);
_viewContext.ExecutingFilePath = _pageContext.ActionDescriptor.RelativePath;

_page = (Page)CacheEntry.PageFactory(_pageContext, _viewContext);

Expand Down Expand Up @@ -265,6 +266,7 @@ private async Task InvokeHandlerMethodAsync()
_tempDataFactory.GetTempData(_pageContext.HttpContext),
TextWriter.Null,
_htmlHelperOptions);
_viewContext.ExecutingFilePath = _pageContext.ActionDescriptor.RelativePath;
}

if (_page == null)
Expand Down
24 changes: 24 additions & 0 deletions test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,30 @@ public async Task PageFiltersAppliedToPageModel_AreExecuted()
Assert.Equal(expected, response.Trim());
}

[Fact]
public async Task ViewLocalizer_WorksForPagesWithoutModel()
{
// Arrange
var expected = "Bon Jour from Page";

// Act
var response = await Client.GetStringAsync("/Pages/Localized/Page?culture=fr-FR");

Assert.Equal(expected, response.Trim());
}

[Fact]
public async Task ViewLocalizer_WorksForPagesWithModel()
{
// Arrange
var expected = "Bon Jour from PageWithModel";

// Act
var response = await Client.GetStringAsync("/Pages/Localized/PageWithModel?culture=fr-FR");

Assert.Equal(expected, response.Trim());
}

private async Task AddAntiforgeryHeaders(HttpRequestMessage request)
{
var getResponse = await Client.GetAsync(request.RequestUri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,32 @@ public async Task InvokeAction_WithPageModel_FlowsRightValues()
Assert.Same(pageModel.PageContext.ViewData, pageResult.ViewData);
}

[Fact]
public async Task InvokeAction_WithPage_SetsExecutingFilePath()
{
// Arrange
var relativePath = "/Pages/Users/Show.cshtml";
var descriptor = CreateDescriptorForSimplePage();
descriptor.RelativePath = relativePath;

object instance = null;
var pageFilter = new Mock<IPageFilter>();
AllowSelector(pageFilter);
pageFilter
.Setup(f => f.OnPageHandlerExecuting(It.IsAny<PageHandlerExecutingContext>()))
.Callback<PageHandlerExecutingContext>(c =>
{
instance = c.HandlerInstance;
});
var invoker = CreateInvoker(new[] { pageFilter.Object }, descriptor);

// Act
await invoker.InvokeAsync();

// Assert
var page = Assert.IsType<TestPage>(instance);
Assert.Equal(relativePath, page.ViewContext.ExecutingFilePath);
}
#endregion

#region Handler Selection
Expand Down
3 changes: 3 additions & 0 deletions test/WebSites/RazorPagesWebSite/Pages/Localized/Page.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@page
@inject Microsoft.AspNetCore.Mvc.Localization.IViewLocalizer Localizer
@Localizer["Hello"]
18 changes: 18 additions & 0 deletions test/WebSites/RazorPagesWebSite/Pages/Localized/Page.fr-FR.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Hello" xml:space="preserve">
<value>Bon Jour from Page</value>
</data>
</root>
13 changes: 13 additions & 0 deletions test/WebSites/RazorPagesWebSite/Pages/Localized/PageWithModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace RazorPagesWebSite.Pages.Localized
{
public class PageWithModel : PageModel
{
public IActionResult OnGet() => Page();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@page
@model PageWithModel
@inject Microsoft.AspNetCore.Mvc.Localization.IViewLocalizer Localizer
@Localizer["Hello"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Hello" xml:space="preserve">
<value>Bon Jour from PageWithModel</value>
</data>
</root>
15 changes: 14 additions & 1 deletion test/WebSites/RazorPagesWebSite/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Globalization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -13,7 +14,7 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options => options.LoginPath = "/Login");
services.AddMvc()
.AddCookieTempDataProvider()
.AddViewLocalization()
.AddRazorPagesOptions(options =>
{
options.Conventions.AuthorizePage("/HelloWorldWithAuth");
Expand All @@ -31,6 +32,18 @@ public void Configure(IApplicationBuilder app)

app.UseStaticFiles();

var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("fr-FR"),
};

app.UseRequestLocalization(new RequestLocalizationOptions
{
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
});

app.UseMvc();
}
}
Expand Down

0 comments on commit c13cef6

Please sign in to comment.