Ale*_*lex 5 c# asp.net-core razor-pages
我正在开发一个小型Web应用程序(Razor Pages),我已经添加了本地化.我现在遇到的问题如下:
当应用程序第一次加载或用户按下主页按钮时(<a href="/"</a>),浏览器中的URL为:
https://localhost/
当我按下链接时,(<a asp-page="/login"></a>)它会导航我https://localhost/login而不是https://localhost/{currentCulture}/login
因此,我希望它是这样的:
https://localhost/{currentCulture}/
例如,对于英语 - > https://localhost/en/
我已经设置了默认的当前文化,它在应用程序启动时应用,但不会在URL中写入.
我按照这个教程本地化添加到我的应用程序:http://www.ziyad.info/en/articles/10-Developing_Multicultural_Web_Application
更新:
当用户按下主页按钮时,我这样做了:<a href="/@CultureInfo.CurrentCulture.Name"></a>它有效.
我不知道它的解决方案有多好,但你可以这样解决这个问题:
创建一个类并实现Microsoft.AspNetCore.Rewrite.IRule:
public class FirstLoadRewriteRule : IRule
{
public void ApplyRule(RewriteContext context)
{
var culture = CultureInfo.CurrentCulture;
var request = context.HttpContext.Request;
if(request.Path == "/")
{
request.Path = new Microsoft.AspNetCore.Http.PathString($"/{culture.Name}");
}
}
}
Run Code Online (Sandbox Code Playgroud)
在您的应用程序中,request.Path == "/"仅当应用程序首次加载时才为 true(当您按 Home 时,request.path 为“/en”{for English})。因此,默认区域性名称将添加到 url 中。当应用程序加载时,您不会在 url 中看到它,但是当您按 时(<a asp-page="/login"></a>),您会看到您被重定向到https://localhost/en/login。
您必须在方法startup.cs中注册此规则Configure:
var options = new RewriteOptions().Add(new FirstLoadRewriteRule());
app.UseRewriter(options);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
143 次 |
| 最近记录: |