当我显示联系人的详细信息时,URL看起来像这样
https://localhost: 44379/ContactsOverview/细节吗?id = -1455774161
当我通过culture switch下拉菜单改变区域性时URL扩展为
https://localhost: 44379/ContactsOverview/细节吗?Culture =en并覆盖我的?id=-1455774161
当我尝试手动URL的组合,如
https://localhost: 44379/ContactsOverview/细节? id = -1455774161 ?区域性=如果无法检索该页
同样在https://localhost:44379/ContactsOverview/Details?culture=en?id=-1455774161上也不起作用
我尝试了@page with "id:min(1)}/{handler?}"它也不起作用。我怎么解它?
====================================================== 更新3.2.2021 = = = = =
我阅读了这篇文章并根据https://www.mikesdotnetting.com/article/348/razor-pages-localisation-seo-friendly-urls
实现====示例代码=====
using Microsoft.AspNetCore.Mvc.ApplicationModels;
namespace Localisation.RouteModelConventions
{
public class CultureTemplatePageRouteModelConvention : IPageRouteModelConvention
{
public void Apply(PageRouteModel model)
{
var selectorCount = model.Selectors.Count;
for (var i = 0; i < selectorCount; i++)
{
var selector = model.Selectors[i];
model.Selectors.Add(new SelectorModel
{
AttributeRouteModel = new AttributeRouteModel
{
Order = -1,
Template = AttributeRouteModel.CombineTemplates("{culture?}", selector.AttributeRouteModel.Template),
}
});
}
}
}
}
和添加在启动
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages(options => {
options.Conventions.Add(new CultureTemplatePageRouteModelConvention());
});
}
结果我实现了url手动工作,就像我想要的
https://localhost: 44379/EN/联系人/编辑吗?id = -1455774161
和页面开始,一切似乎都很酷:-)
现在我尝试按照描述实现Razor页面url调用
我尝试了不同的方法。然而,非工作…任何帮助吗?
我尝试过的样本
======== Index Razor Page ================
@page
@using Microsoft.AspNetCore.Localization
@using Microsoft.AspNetCore.Mvc.Localization
@model WorkCollaboration.Pages.ContactsOverview.IndexModel
@{
ViewData["Title"] = "Index";
var requestCultureFeature = HttpContext.Features.Get<IRequestCultureFeature>();
var requestCulture = requestCultureFeature.RequestCulture;
}
@inject IViewLocalizer Localizer
@await Component.InvokeAsync("NToastNotify")
<h1>@Localizer["Index"]</h1>
<h4>@Localizer["Contacts Overview"]</h4>
<p>
<a asp-page="/Contacts/Create">@Localizer["Create New"]</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.ContactsOverview[0].ContactId)<br />
@Html.DisplayNameFor(model => model.ContactsOverview[0].FirstName)
@Html.DisplayNameFor(model => model.ContactsOverview[0].LastName)<br />
</th>
<th>
@Html.DisplayNameFor(model => model.ContactsOverview[0].SupId)
@Html.DisplayNameFor(model => model.ContactsOverview[0].SupName)<br />
</th>
<th>
@Html.DisplayNameFor(model => model.ContactsOverview[0].CusId)
@Html.DisplayNameFor(model => model.ContactsOverview[0].CusName)<br />
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.ContactsOverview)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.ContactId)<br>
@Html.DisplayFor(modelItem => item.FirstName)
@Html.DisplayFor(modelItem => item.LastName)<br>
</td>
<td>
@Html.DisplayFor(modelItem => item.SupId)
</td>
<td>
@Html.DisplayFor(modelItem => item.CusId)
@Html.DisplayFor(modelItem => item.CusName)<br>
</td>
<td>
@*<a asp-route-culture="@Request.RouteValues["culture"]" asp-page="@Localizer["UrlEdit"]" asp-route-id="@item.ContactId">@Localizer["Edit"]</a>
<a asp-route-culture="@Request.RouteValues["culture"]" asp-page="@Localizer.GetString("UrlEdit")">Test</a>
<a asp-page="/EN/Contacts/Edit" asp-route-id="@item.ContactId">@Localizer["Edit"]</a>*@
<a asp-page="/Contacts/Edit" asp-route-id="@item.ContactId">@Localizer["Edit"]</a>
<a asp-page="/ContactsOverview/Details" asp-route-id="@item.ContactId">@Localizer["Details"]</a>
<a asp-page="/Contacts/Delete" asp-route-id="@item.ContactId">@Localizer["Delete"]</a>
</td>
</tr>
}
</tbody>
</table>
第一个变体是直的我把url放进
<a asp-page="/EN/Contacts/Edit" asp-route-id="@item.ContactId">@Localizer["Edit"]</a>
: - (我试图使用我的资源文件连接URL
<a asp-route-culture="@Request.RouteValues["culture"]" asp-page="@Localizer["UrlEdit"]" asp-route-id="@item.ContactId">@Localizer["Edit"]</a>
第三次静脉注射无效
<a asp-route-culture="@Request.RouteValues["culture"]" asp-page="@Localizer.GetString("UrlEdit")">Test</a>
任何想法?
在描述中引用脚本
<script>
var segments = location.pathname.split('/');
var el = document.getElementById("culture-options");
el.addEventListener("change", () => {
var culture = el.options[el.selectedIndex].value;
if (segments.length > 2) {
segments[1] = culture;
location.href = segments.join('/');
} else {
location.href = '/' + culture + location.pathname;
}
});
</script>
我真不知道该把它放在哪里。我的想法是从commonswitchclass实现中替换默认的。但是这也不起作用,因为url中的?id被截断了
非常感谢你的帮助
这个描述工作很多其他的不
Asp.net core Localization总是返回英语文化
: - (