ASP.NET Core 2.2网站显示了来自Kentico Cloud CMS的数据。其中一些项目包含富文本字段。这些字段可能包含指向其他内容项的链接。这些链接应由网站解析为网址。
此docs建议通过实现IContentLinkUrlResolver
接口来完成此操作。但是,对于我们的网站而言,该样本实在是太虚伪了。我们的解析器需要知道请求的上下文(例如当前的UI文化,因为该站点是多语言环境)和路由定义,因为这是定义URL外观的唯一位置。
实际上,解析程序需要能够调用Url.Action,就像它可以在视图内部一样。
URL解析器应尊重路由和当前的ui文化。预期其逻辑如下:
if (the linked content item type is Page)
{
Url.Action("Page", "Home", new [] { codename = content item’s codename });
}
else if (the linked content item type is PageFont)
{
Url.Action("Font", "Home", new [] { codename = content item’s codename });
}
else
{
throw an error about an unsupported content type.
}
规则定义为:
var localizationOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRouter(routes =>
{
routes.MapMiddlewareRoute("{culture=en-US}/{*mvcRoute}", subApp =>
{
subApp.UseRequestLocalization(localizationOptions.Value);
subApp.UseMvc(mvcRoutes =>
{
mvcRoutes.MapRoute(
name: "areas",
template: "{culture=en-US}/{area:exists}/{controller=Home}/{action=Index}/{id?}");
mvcRoutes.MapRoute(
name: "sitemap",
defaults: new { controller = "Sitemap", action = "Index" },
template: "sitemap.xml");
mvcRoutes.MapRoute(
name: "font",
defaults: new { controller = "Home", action = "Font" },
template: "{culture=en-US}/font/{codename}");
mvcRoutes.MapRoute(
name: "page",
defaults: new { controller = "Home", action = "Page" },
template: "{culture=en-US}/{codename}");
mvcRoutes.MapRoute(
name: "default",
template: "{culture=en-US}/{controller=Home}/{action=Index}/{id?}");
});
});
});
如何解析与站点配置中的路由相关的链接?
答案 0 :(得分:5)
这应该能够在视图中解析类似于Url.Action
的URL:
在Startup.cs
中:
services
.TryAddSingleton<IActionContextAccessor, ActionContextAccessor>();
services
.AddSingleton<IContentLinkUrlResolver, RoutesContentLinkUrlResolver>()
.AddDeliveryClient( ... )
;
在RoutesContentLinkUrlResolver.cs
中:
private readonly IUrlHelperFactory urlHelperFactory;
private readonly IActionContextAccessor actionContextAccessor;
public RoutesContentLinkUrlResolver(IUrlHelperFactory urlHelperFactory, IActionContextAccessor actionContextAccessor)
{
this.urlHelperFactory = urlHelperFactory;
this.actionContextAccessor = actionContextAccessor;
}
public string ResolveLinkUrl(ContentLink link)
{
IUrlHelper Url = urlHelperFactory.GetUrlHelper(actionContextAccessor.ActionContext);
switch (link.ContentTypeCodename)
{
case "Page":
return Url.Action("Page", "Home", new { codename = link.Codename });
}
}
IUrlHelper
的实例要尽可能晚地检索,以确保它具有最新的运行时值。 IUrlHelperFactory
的默认实现使用缓存来提高性能:UrlHelperFactory line 44。