You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Razor Pages配置为/en/这类文化本地化路由格式?

配置Razor Pages实现基于URL路径的多语言文化路由

我之前也碰到过类似的Razor Pages多语言路由配置问题,折腾了好一阵才弄好,给你整理一套靠谱的步骤,应该能解决你的问题:

1. 配置本地化服务和请求本地化选项

首先在Program.cs里添加本地化服务,并设置支持的文化,同时指定从URL路径提取文化信息:

// 添加本地化服务,指定资源文件存放路径
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");

// 配置请求本地化
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
    // 定义支持的语言文化
    var supportedCultures = new[]
    {
        new CultureInfo("en"),
        new CultureInfo("ru")
    };

    // 设置默认文化为英文
    options.DefaultRequestCulture = new RequestCulture("en");
    options.SupportedCultures = supportedCultures;
    options.SupportedUICultures = supportedCultures;

    // 优先从URL路由的culture参数提取文化信息
    options.RequestCultureProviders.Insert(0, new RouteDataRequestCultureProvider
    {
        RouteDataStringKey = "culture",
        Options = options
    });
});

2. 配置Razor Pages路由模板,添加文化前缀

接下来要给所有Razor Pages页面添加文化前缀的路由,同时处理无文化前缀的默认访问:

app.UseStaticFiles(); // 先处理静态文件,确保css/js等资源不需要带文化前缀

app.UseRouting();

// 启用请求本地化中间件,要放在路由之后、端点之前
app.UseRequestLocalization();

app.UseEndpoints(endpoints =>
{
    endpoints.MapRazorPages(options =>
    {
        // 给根目录下的所有页面添加文化前缀路由模板
        options.Conventions.AddFolderRouteModelConvention("/", model =>
        {
            foreach (var selector in model.Selectors)
            {
                var originalTemplate = selector.AttributeRouteModel.Template;
                // 把文化参数作为路由前缀,同时添加约束只允许en/ru
                selector.AttributeRouteModel.Template = "{culture:culture=en|ru}/" + (originalTemplate ?? "");
            }
        });

        // 配置默认路由:当访问根路径时,默认映射到英文版本
        options.Conventions.AddPageRouteModelConvention("/Index", model =>
        {
            model.Selectors.Add(new SelectorModel
            {
                AttributeRouteModel = new AttributeRouteModel
                {
                    Template = "",
                    // 可选:设置自动重定向到/en/
                    // RedirectToRoute = new RouteValueDictionary { { "culture", "en" } }
                }
            });
        });
    });
});

3. 处理页面内的文化切换

如果需要在页面上添加语言切换按钮/链接,可以直接使用asp-route-culture生成正确的URL:

<!-- 在_Layout.cshtml或者其他页面添加切换链接 -->
<div class="language-switcher">
    <a asp-page="/Index" asp-route-culture="en">English</a>
    <a asp-page="/Index" asp-route-culture="ru">Русский</a>
</div>

4. 准备本地化资源文件

在项目根目录创建Resources文件夹,添加对应页面的资源文件,比如:

  • Index.en.resx(英文资源)
  • Index.ru.resx(俄文资源)

然后在页面里使用@Localizer来获取本地化内容:

@page
@model IndexModel
@inject IStringLocalizer<IndexModel> Localizer

<h1>@Localizer["Welcome"]</h1>

常见坑点提醒

  • 中间件顺序不能乱:UseStaticFiles要放在UseRouting之前,否则静态资源会被要求带上文化前缀;UseRequestLocalization要放在UseRouting之后、UseEndpoints之前。
  • 路由约束要明确:{culture:culture=en|ru}里的culture约束会自动验证文化格式,同时限定只能是en或ru。
  • 默认路由处理:如果希望访问根路径时自动重定向到默认文化,可以在默认路由的RedirectToRoute里配置,否则会直接渲染默认文化的页面。

按照这套配置,你应该就能实现https://localhost:44396/en/和https://localhost:44396/ru/的路由格式了,我之前用这套配置解决了类似的问题,应该没问题。

内容的提问来源于stack exchange,提问作者Agon Noga

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:49:28