public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHttpsRedirection();
}
var SupportedCultures = new List<CultureInfo> {
new CultureInfo("en"),
new CultureInfo("zh-Hans"),
new CultureInfo("zh-Hant")
};
var options = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en"),
SupportedCultures = SupportedCultures,
SupportedUICultures = SupportedCultures
};
app.UseRequestLocalization(options);
app.UseStaticFiles();
app.UseStatusCodePagesWithReExecute("{culture}/StatusCode/{0}");
var requestProvider = new RouteDataRequestCultureProvider();
options.RequestCultureProviders.Insert(0, requestProvider);
app.UseRouter(routes =>
{
routes.MapMiddlewareRoute("{culture=en}/{*mvcRoute}", subApp =>
{
subApp.UseRequestLocalization(options);
subApp.UseRouting();
subApp.UseEndpoints(mvcRoutes =>
{
mvcRoutes.MapControllerRoute(
name: "default",
pattern: "{culture=en}/{controller=Home}/{action=Index}");
// map the .html suffix
mvcRoutes.MapControllerRoute(
name: "home.html",
pattern: "{culture=en}/{controller=Home}/{action}.html");
});
});
});
}
And here is the StatusCodeController:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace Test.Controllers
{
public class StatusCodeController : Controller
{
[HttpGet("{culture}/StatusCode/{statusCode}")]
public IActionResult Index(int statusCode)
{
return View(statusCode);
}
}
}
The Localizer works well.
Whereas, when I navigate a URL which the page does not exist. It will only redirect to the default language 404Page, such as http://localhost:5009/en/StatusCode/404
I have added the culture property inside the app.UseStatusCodePagesWithReExecute but it seems no works.
You could not use the placeholder "{culture}" directly in the UseStatusCodePagesWithReExecute() method. You need to add a new StatusCodePages middleware to the pipeline to get the current culture.
This is the method of redirection:
app.UseStatusCodePages(async context =>
{
var currentCulture = CultureInfo.CurrentCulture.Name;
var redirectPath = "/" + currentCulture + "/StatusCode/" + context.HttpContext.Response.StatusCode;
context.HttpContext.Response.Redirect(redirectPath);
});
Member
53 Points
246 Posts
How can I make the StatusCodePages support Localizer?
Sep 17, 2019 09:05 AM|mywatermelon|LINK
Here is the Configure:
And here is the StatusCodeController:
The Localizer works well.
Whereas, when I navigate a URL which the page does not exist. It will only redirect to the default language 404Page, such as http://localhost:5009/en/StatusCode/404
I have added the culture property inside the app.UseStatusCodePagesWithReExecute but it seems no works.
How can I solve this? Thank you.
Member
170 Points
88 Posts
Re: How can I make the StatusCodePages support Localizer?
Sep 19, 2019 06:40 AM|Lewis Lu|LINK
Hi mywatermelon,
You could not use the placeholder "{culture}" directly in the UseStatusCodePagesWithReExecute() method. You need to add a new StatusCodePages middleware to the pipeline to get the current culture.
This is the method of redirection:
This is the method of rewriting:
Best Regards ,
Lewis Lu
Member
53 Points
246 Posts
Re: How can I make the StatusCodePages support Localizer?
Sep 19, 2019 08:07 AM|mywatermelon|LINK
No, it doesn't work.
I added a breakpoint in the Visual Studio. When running a page which not exists,
The codes above always get "en" even the URL is 'http://localhost:5009/zh-Hans/product/555.html' which the Localizer is zh-hans.
Member
170 Points
88 Posts
Re: How can I make the StatusCodePages support Localizer?
Sep 19, 2019 10:01 AM|Lewis Lu|LINK
Hi mywatermelon,
Are you missing the method which you changed your CurrentCulture in you project ? here is my way,I put it in the index method of the home controller.
Best Regards ,
Lewis Lu
Member
53 Points
246 Posts
Re: How can I make the StatusCodePages support Localizer?
Sep 19, 2019 10:39 AM|mywatermelon|LINK
It works now. However, it needs to add the method inside every controller.
I remembered that the URL also store the Culture in my project, so I modified the code like this:
It works. Aha.