区域/语言页面路由约定URL生成

时间:2019-03-30 18:47:59

标签: c# asp.net-core razor-pages

我有以下模板:"{region}/{language}-{region}"

示例网址匹配:

  • domain.com/us/en-us
  • domain.com/ca/en-ca
  • domain.com/ca/fr-ca

我在RegionLanguagePageRouteModelConvention实现中使用此模板,如下所示:

public class RegionLanguagePageRouteModelConvention : IPageRouteModelConvention
{
    public void Apply(PageRouteModel model)
    {
        var selectorCount = model.Selectors.Count;
        for (var i = 0; i < selectorCount; i++)
        {
            var selector = model.Selectors[i];

            selector.AttributeRouteModel.Template = AttributeRouteModel.CombineTemplates("{region}/{language}-{region}", selector.AttributeRouteModel.Template);                
        }
    }
}

然后我将自定义约定添加到Startup.cs中的约定集合中,如下所示:

services.AddMvc()
   .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
   .AddRazorPagesOptions(options =>
       options.Conventions.Add(new RegionLanguagePageRouteModelConvention()));

接下来,我想在我的Razor页面中生成一个链接。我尝试这样的事情:

<a asp-page="privacy">Link to default Privacy page in a Razor Page app</a>

我希望将锚标记呈现为:

<a href="/us/en-us/privacy">Link to default Privacy page in a Razor Page app</a>

但是,完全无法掌握URL生成工具/ API来生成这些类型的链接的情况。

我该如何实现?

0 个答案:

没有答案