ASP.基于.NET Core MVC路由的带有区域的本地化



我在ASP中创建了一个项目。并希望基于url来检测该语言:https://localhost: 7090/en

如果在url中使用了控制器和动作,那么一切都会按计划进行。然而,当注册或登录页面被调用时,它不起作用(默认的asp.net身份注册页面)。

工作:https://localhost: 7090/en/Home/指数

不工作:https://localhost:7090/en/Identity/Account/Register

在启动时,我为MVC路由配置如下:

builder.Services
.AddLocalization()
.AddMvc(options => options.EnableEndpointRouting = false);
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = CultureHelper.GetSupportedCultures();
options.DefaultRequestCulture = new RequestCulturne("en");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
var provider = new RouteDataRequestCultureProvider
{
RouteDataStringKey = "culture",
UIRouteDataStringKey = "culture",
Options = options
};
options.RequestCultureProviders = new[] { provider };
});
builder.Services.Configure<RouteOptions>(options =>
{
options.ConstraintMap.Add("culture", typeof(LanguageRouteConstraint));
});

var options = app.Services.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(options.Value); 
app.UseMvc(routes =>
{
app.MapRazorPages();
routes.MapRoute(
name: "LocalizedDefault",
template: "{culture:culture}/{controller=Home}/{action=Index}/{id?}"
);
});

然后使用语言约束来设置CurrentCulture和currentuicculture。

下面是调用登录/注册页面的代码片段:
<li class="nav-item">
<a class="nav-link" asp-area="Identity" asp-page="/Account/Register">
@Language.register
</a>
</li>
<li class="nav-item">
<a class="nav-link" asp-area="Identity" asp-page="/Account/Login">
@Language.login
</a>
</li>

我已经尝试了几乎所有我在谷歌上找到的东西,但似乎没有工作....

我只是认为知道它不起作用是因为页面

(更新)我能够使用以下代码使url有效:

builder.Services
.AddLocalization(options => options.ResourcesPath = "Resources")
.AddMvc(options => options.EnableEndpointRouting = false)
.AddRazorPagesOptions(options =>
{
options.Conventions.Add(new LanguageRouteModelConversion());
});

public class LanguageRouteModelConversion : IPageRouteModelConvention
{
public void Apply(PageRouteModel pageRouteModel)
{
var selectorModels = new List<SelectorModel>();

foreach (var selector in pageRouteModel.Selectors.ToList())
{
var template = selector.AttributeRouteModel?.Template;
selectorModels.Add(new SelectorModel()
{
AttributeRouteModel = new AttributeRouteModel
{
Template = "/{culture}/" + template
}
});
}
foreach (var model in selectorModels)
pageRouteModel.Selectors.Add(model);
}
}

但我仍然不知道如何正确调用页面

您说您的Identity目录中没有Account文件夹。如果是这样,那么您如何期望register,login页面路由器将工作?

builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<YourDbContextName>();

然后Scaffold注册,登录,注销页面等。搭建完成后,您将在Pages文件夹中找到一个单独的Account文件夹。您的Account文件夹实际上包含登录,注册页面。这应该能解决你的问题。

builder.Services
.AddLocalization(options => options.ResourcesPath = "Resources")
.AddMvc(options => options.EnableEndpointRouting = false)
.AddRazorPagesOptions(options =>
{
options.Conventions.AddAreaFolderRouteModelConvention("Identity", "/", pageRouteModel =>
{
foreach (var selectorModel in pageRouteModel.Selectors)
selectorModel.AttributeRouteModel.Template = "{culture:culture}/" + selectorModel.AttributeRouteModel.Template;
});
});

这对我来说很有用,我终于弄明白了…

将文化参数添加到URLasp-route-culture=...

@{ var culture= System.Globalization.CultureInfo.CurrentCulture.Name }
<li class="nav-item">
<a class="nav-link" asp-area="Identity" asp-page="/Account/Register" asp-route-culture="@culture">
@Language.register
</a>
</li>
<li class="nav-item">
<a class="nav-link" asp-area="Identity" asp-page="/Account/Login" asp-route-culture="@culture">
@Language.login
</a>
</li>

相关内容

  • 没有找到相关文章

最新更新