dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.21k stars 9.95k forks source link

Adding feature to Router for custom language URL #40736

Open emir3100 opened 2 years ago

emir3100 commented 2 years ago

Is there an existing issue for this?

Is your feature request related to a problem? Please describe the problem.

Im using blazor server and when I am trying to implement a language url to my website, I run into problems with the router component.

I tried to modify the current router but many of the classes are protected and inaccessible.

https://example.com/home --> https://example.com/en/home My desired outcome is to use @page "/home" on the pages without adding the language code.

Describe the solution you'd like

Either implement a feature to easier create language URL with the router component or make it easier to modify the current router.

Additional context

No response

Alerinos commented 2 years ago
                endpoints.MapGet("/{culture:regex(^[a-zA-Z]{{2}}(-[a-zA-Z+]{{2}})?$)=pl}/{**rest}", async context =>
                {
                    var culture = context.Request.RouteValues["culture"] ?? _website.Culture;
                    var rest = context.Request.RouteValues["rest"];

                    var option = new CookieOptions
                    {
                        Expires = DateTime.Now.AddYears(1)
                    };
                    context.Response.Cookies.Append("Culture", culture.ToString() ?? "", option);

                    context.Response.Redirect($"/{rest}", permanent: false);

                    await Task.CompletedTask;
                });

The only thing you can do is redirect the URL to the cookie. Routing in blazor is very weak.

javiercn commented 2 years ago

Tracking as part of https://github.com/dotnet/aspnetcore/issues/38121

danroth27 commented 1 year ago

Reopening so we can independently consider this link generation scenario.