MVC 路由模板来表示无限self-referential 层级类别结构
MVC Routing template to represent infinite self-referential hierarchical category structure
我有一个产品类别table来表示一个层次结构的类别结构,在数据库中是一个典型的Parent-Child
关系table。
以Guitar Center的数据为例填写:
如果将它们呈现到具有 <ul>
和 <li>
的页面:
蓝色文本是我要生成的 URL。对于任何给定的类别,link 由它的 slug 和它的 parents' slug 组成。
请注意,我列出的示例只有 2 个 parent-child 级别。理论上,使用 self-referential 结构,任何 child 都可以有无限的 parents.
问题:
- 如何设置路由模板来实现?
- 如果路由模板设置支持,如何检索叶子类别?例如,从 URL
categories/guitars/acoustic-guitars
中,我想检索 acoustic-guitars
作为叶子类别,并能够获取该 acoustic-guitars
类别下的所有产品。注意:我不想在 URL 上手动解析。理想情况下,如果引导类别通过模型绑定进行绑定,那将是最好的。
How to set up routing template to achieve that?
你不能。但是你可以降到一个较低的层次,为CMS风格的路由管理做一个数据驱动的IRouter
实现。
一个例子:CachedRoute<TPrimaryKey>
这是一个跟踪和缓存主键到 URL 的 1-1 映射的示例。它是一个通用的 class,我已经测试过它可以工作,无论主键是 int 还是 Guid。
有一个必须注入的可插入部分,ICachedRouteDataProvider
可以在其中实现对数据库的查询。您还需要提供控制器和操作,因此该路由足够通用,可以通过使用多个实例将多个数据库查询映射到多个操作方法。
public class CachedRoute<TPrimaryKey> : Microsoft.AspNetCore.Routing.IRouter
{
private readonly string _controller;
private readonly string _action;
private readonly ICachedRouteDataProvider<TPrimaryKey> _dataProvider;
private readonly IMemoryCache _cache;
private readonly IRouter _target;
private readonly string _cacheKey;
private object _lock = new object();
public CachedRoute(
string controller,
string action,
ICachedRouteDataProvider<TPrimaryKey> dataProvider,
IMemoryCache cache,
IRouter target)
{
if (string.IsNullOrWhiteSpace(controller))
throw new ArgumentNullException("controller");
if (string.IsNullOrWhiteSpace(action))
throw new ArgumentNullException("action");
if (dataProvider == null)
throw new ArgumentNullException("dataProvider");
if (cache == null)
throw new ArgumentNullException("cache");
if (target == null)
throw new ArgumentNullException("target");
_controller = controller;
_action = action;
_dataProvider = dataProvider;
_cache = cache;
_target = target;
// Set Defaults
CacheTimeoutInSeconds = 900;
_cacheKey = "__" + this.GetType().Name + "_GetPageList_" + _controller + "_" + _action;
}
public int CacheTimeoutInSeconds { get; set; }
public async Task RouteAsync(RouteContext context)
{
var requestPath = context.HttpContext.Request.Path.Value;
if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == '/')
{
// Trim the leading slash
requestPath = requestPath.Substring(1);
}
// Get the page id that matches.
//If this returns false, that means the URI did not match
if (!GetPageList(context.HttpContext).TryGetValue(requestPath, out TPrimaryKey id))
{
return;
}
//Invoke MVC controller/action
var routeData = context.RouteData;
// TODO: You might want to use the page object (from the database) to
// get both the controller and action, and possibly even an area.
// Alternatively, you could create a route for each table and hard-code
// this information.
routeData.Values["controller"] = _controller;
routeData.Values["action"] = _action;
// This will be the primary key of the database row.
// It might be an integer or a GUID.
routeData.Values["id"] = id;
await _target.RouteAsync(context);
}
public VirtualPathData GetVirtualPath(VirtualPathContext context)
{
VirtualPathData result = null;
if (TryFindMatch(GetPageList(context.HttpContext), context.Values, out string virtualPath))
{
result = new VirtualPathData(this, virtualPath);
}
return result;
}
private bool TryFindMatch(IDictionary<string, TPrimaryKey> pages, IDictionary<string, object> values, out string virtualPath)
{
virtualPath = string.Empty;
TPrimaryKey id;
if (!values.TryGetValue("id", out object idObj))
{
return false;
}
id = SafeConvert<TPrimaryKey>(idObj);
values.TryGetValue("controller", out object controller);
values.TryGetValue("action", out object action);
// The logic here should be the inverse of the logic in
// RouteAsync(). So, we match the same controller, action, and id.
// If we had additional route values there, we would take them all
// into consideration during this step.
if (action.Equals(_action) && controller.Equals(_controller))
{
// The 'OrDefault' case returns the default value of the type you're
// iterating over. For value types, it will be a new instance of that type.
// Since KeyValuePair<TKey, TValue> is a value type (i.e. a struct),
// the 'OrDefault' case will not result in a null-reference exception.
// Since TKey here is string, the .Key of that new instance will be null.
virtualPath = pages.FirstOrDefault(x => x.Value.Equals(id)).Key;
if (!string.IsNullOrEmpty(virtualPath))
{
return true;
}
}
return false;
}
private IDictionary<string, TPrimaryKey> GetPageList(HttpContext context)
{
if (!_cache.TryGetValue(_cacheKey, out IDictionary<string, TPrimaryKey> pages))
{
// Only allow one thread to poplate the data
lock (_lock)
{
if (!_cache.TryGetValue(_cacheKey, out pages))
{
pages = _dataProvider.GetPageToIdMap(context.RequestServices);
_cache.Set(_cacheKey, pages,
new MemoryCacheEntryOptions()
{
Priority = CacheItemPriority.NeverRemove,
AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(this.CacheTimeoutInSeconds)
});
}
}
}
return pages;
}
private static T SafeConvert<T>(object obj)
{
if (typeof(T).Equals(typeof(Guid)))
{
if (obj.GetType() == typeof(string))
{
return (T)(object)new Guid(obj.ToString());
}
return (T)(object)Guid.Empty;
}
return (T)Convert.ChangeType(obj, typeof(T));
}
}
CategoryCachedRouteDataProvider
这里我们从数据库中查找类别,并递归地将 slug 连接到 URL。
public interface ICachedRouteDataProvider<TPrimaryKey>
{
IDictionary<string, TPrimaryKey> GetPageToIdMap(IServiceProvider serviceProvider);
}
public class CategoryCachedRouteDataProvider : ICachedRouteDataProvider<int>
{
// NOTE: I wasn't able to figure out how to constructor inject ApplicationDbContext
// because there doesn't seem to be a way to access the scoped services there,
// so we are using a service locator here. If someone could let me know how
// that is done in Startup.Configure() of .NET Core 2.0, please leave a comment.
public IDictionary<string, int> GetPageToIdMap(IServiceProvider serviceProvider)
{
using (var dbContext = serviceProvider.GetRequiredService<ApplicationDbContext>())
{
// Query the categories so we can build all of the URLs client side
var categories = dbContext.Categories.ToList();
var scratch = new StringBuilder();
return (from category in categories
select new KeyValuePair<string, int>(
GetUrl(category, categories, scratch),
category.CategoryId)
).ToDictionary(pair => pair.Key, pair => pair.Value);
}
}
private string GetUrl(Category category, IEnumerable<Category> categories, StringBuilder result)
{
result.Clear().Append(category.Slug);
while ((category = categories.FirstOrDefault(c => c.CategoryId == category.ParentCategoryId)) != null)
{
result.Insert(0, string.Concat(category.Slug, "/"));
}
return result.ToString();
}
}
类别控制器
除了我们此时根本不需要处理 URL 或 slug 之外,控制器中没有发生任何特殊情况。我们只需接受映射到记录主键的 id
参数,然后您就知道该怎么做了...
public class CategoryController : Controller
{
public IActionResult Index(int id)
{
// Lookup category based on id...
return View();
}
}
用法
我们在Startup.cs
中配置如下:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddMvc();
services.AddSingleton<CategoryCachedRouteDataProvider>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.Routes.Add(
new CachedRoute<int>(
controller: "Category",
action: "Index",
dataProvider: app.ApplicationServices.GetRequiredService<CategoryCachedRouteDataProvider>(),
cache: app.ApplicationServices.GetRequiredService<IMemoryCache>(),
target: routes.DefaultHandler)
{
CacheTimeoutInSeconds = 900
});
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
请注意,CachedRoute<TPrimaryKey>
可以重复用于为其他 table 创建额外的路由。因此,如果您愿意,还可以通过在类别 table 上使用联接并使用类似的方法构建 URL 来使您的产品 URL 像 guitars/acoustic-guitars/some-fancy-acoustic-guitar
.
可以使用 Tag Helpers 或任何其他基于 UrlHelper
的方法生成 URL 并将其添加到 UI。例如:
<a asp-area="" asp-controller="Category" asp-action="Index" asp-route-id="12">Effects</a>
生成为
<a href="/amps-and-effects/effects">Effects</a>
您当然可以使用模型的主键生成 URL 和 link 的文本 - 使用具有主键和名字.
您唯一需要做的就是为 link 显示创建层次结构。但这不在路由的范围内。
请注意,在路由中根本没有层次结构的概念 - 它只是在每个请求中从上到下匹配的路由列表。
If the routing template is set up to support that, how to retrieve the leaf category? For example, from the URL /guitars/acoustic-guitars, I would like to retrieve acoustic-guitars as the leaf category, and able to get all products under that acoustic-guitars category. Note: I don't want manual parsing on the URL. Ideally it would be the best if the lead category is binded through model binding.
不清楚为什么需要 "leaf category",因为这与传入或传出路由无关,也不需要查找数据库数据。同样,主键是根据路由生成整个 URL 所需的全部内容,它应该是查看所有产品所需的全部内容。但是如果你真的需要访问它,你可以在你的控制器中查找它。
自定义
您可能需要根据您的具体要求更改缓存策略。
- 您可能希望在 RAM
中使用固定最大 link 数量的 LRU 缓存策略
- 您可能希望跟踪 URL 被点击的频率,并将最常访问的 URL 移至列表顶部
- 您可能希望在路由和更新操作方法之间共享缓存,这样当 URLs 在数据库中成功更新时,它们也会同时在缓存中更新 "real-time" URLs
- 您可能希望单独缓存每个 URL 并一次查找一个,而不是一次缓存整个列表
我有一个产品类别table来表示一个层次结构的类别结构,在数据库中是一个典型的Parent-Child
关系table。
以Guitar Center的数据为例填写:
如果将它们呈现到具有 <ul>
和 <li>
的页面:
蓝色文本是我要生成的 URL。对于任何给定的类别,link 由它的 slug 和它的 parents' slug 组成。
请注意,我列出的示例只有 2 个 parent-child 级别。理论上,使用 self-referential 结构,任何 child 都可以有无限的 parents.
问题:
- 如何设置路由模板来实现?
- 如果路由模板设置支持,如何检索叶子类别?例如,从 URL
categories/guitars/acoustic-guitars
中,我想检索acoustic-guitars
作为叶子类别,并能够获取该acoustic-guitars
类别下的所有产品。注意:我不想在 URL 上手动解析。理想情况下,如果引导类别通过模型绑定进行绑定,那将是最好的。
How to set up routing template to achieve that?
你不能。但是你可以降到一个较低的层次,为CMS风格的路由管理做一个数据驱动的IRouter
实现。
一个例子:CachedRoute<TPrimaryKey>
这是一个跟踪和缓存主键到 URL 的 1-1 映射的示例。它是一个通用的 class,我已经测试过它可以工作,无论主键是 int 还是 Guid。
有一个必须注入的可插入部分,ICachedRouteDataProvider
可以在其中实现对数据库的查询。您还需要提供控制器和操作,因此该路由足够通用,可以通过使用多个实例将多个数据库查询映射到多个操作方法。
public class CachedRoute<TPrimaryKey> : Microsoft.AspNetCore.Routing.IRouter
{
private readonly string _controller;
private readonly string _action;
private readonly ICachedRouteDataProvider<TPrimaryKey> _dataProvider;
private readonly IMemoryCache _cache;
private readonly IRouter _target;
private readonly string _cacheKey;
private object _lock = new object();
public CachedRoute(
string controller,
string action,
ICachedRouteDataProvider<TPrimaryKey> dataProvider,
IMemoryCache cache,
IRouter target)
{
if (string.IsNullOrWhiteSpace(controller))
throw new ArgumentNullException("controller");
if (string.IsNullOrWhiteSpace(action))
throw new ArgumentNullException("action");
if (dataProvider == null)
throw new ArgumentNullException("dataProvider");
if (cache == null)
throw new ArgumentNullException("cache");
if (target == null)
throw new ArgumentNullException("target");
_controller = controller;
_action = action;
_dataProvider = dataProvider;
_cache = cache;
_target = target;
// Set Defaults
CacheTimeoutInSeconds = 900;
_cacheKey = "__" + this.GetType().Name + "_GetPageList_" + _controller + "_" + _action;
}
public int CacheTimeoutInSeconds { get; set; }
public async Task RouteAsync(RouteContext context)
{
var requestPath = context.HttpContext.Request.Path.Value;
if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == '/')
{
// Trim the leading slash
requestPath = requestPath.Substring(1);
}
// Get the page id that matches.
//If this returns false, that means the URI did not match
if (!GetPageList(context.HttpContext).TryGetValue(requestPath, out TPrimaryKey id))
{
return;
}
//Invoke MVC controller/action
var routeData = context.RouteData;
// TODO: You might want to use the page object (from the database) to
// get both the controller and action, and possibly even an area.
// Alternatively, you could create a route for each table and hard-code
// this information.
routeData.Values["controller"] = _controller;
routeData.Values["action"] = _action;
// This will be the primary key of the database row.
// It might be an integer or a GUID.
routeData.Values["id"] = id;
await _target.RouteAsync(context);
}
public VirtualPathData GetVirtualPath(VirtualPathContext context)
{
VirtualPathData result = null;
if (TryFindMatch(GetPageList(context.HttpContext), context.Values, out string virtualPath))
{
result = new VirtualPathData(this, virtualPath);
}
return result;
}
private bool TryFindMatch(IDictionary<string, TPrimaryKey> pages, IDictionary<string, object> values, out string virtualPath)
{
virtualPath = string.Empty;
TPrimaryKey id;
if (!values.TryGetValue("id", out object idObj))
{
return false;
}
id = SafeConvert<TPrimaryKey>(idObj);
values.TryGetValue("controller", out object controller);
values.TryGetValue("action", out object action);
// The logic here should be the inverse of the logic in
// RouteAsync(). So, we match the same controller, action, and id.
// If we had additional route values there, we would take them all
// into consideration during this step.
if (action.Equals(_action) && controller.Equals(_controller))
{
// The 'OrDefault' case returns the default value of the type you're
// iterating over. For value types, it will be a new instance of that type.
// Since KeyValuePair<TKey, TValue> is a value type (i.e. a struct),
// the 'OrDefault' case will not result in a null-reference exception.
// Since TKey here is string, the .Key of that new instance will be null.
virtualPath = pages.FirstOrDefault(x => x.Value.Equals(id)).Key;
if (!string.IsNullOrEmpty(virtualPath))
{
return true;
}
}
return false;
}
private IDictionary<string, TPrimaryKey> GetPageList(HttpContext context)
{
if (!_cache.TryGetValue(_cacheKey, out IDictionary<string, TPrimaryKey> pages))
{
// Only allow one thread to poplate the data
lock (_lock)
{
if (!_cache.TryGetValue(_cacheKey, out pages))
{
pages = _dataProvider.GetPageToIdMap(context.RequestServices);
_cache.Set(_cacheKey, pages,
new MemoryCacheEntryOptions()
{
Priority = CacheItemPriority.NeverRemove,
AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(this.CacheTimeoutInSeconds)
});
}
}
}
return pages;
}
private static T SafeConvert<T>(object obj)
{
if (typeof(T).Equals(typeof(Guid)))
{
if (obj.GetType() == typeof(string))
{
return (T)(object)new Guid(obj.ToString());
}
return (T)(object)Guid.Empty;
}
return (T)Convert.ChangeType(obj, typeof(T));
}
}
CategoryCachedRouteDataProvider
这里我们从数据库中查找类别,并递归地将 slug 连接到 URL。
public interface ICachedRouteDataProvider<TPrimaryKey>
{
IDictionary<string, TPrimaryKey> GetPageToIdMap(IServiceProvider serviceProvider);
}
public class CategoryCachedRouteDataProvider : ICachedRouteDataProvider<int>
{
// NOTE: I wasn't able to figure out how to constructor inject ApplicationDbContext
// because there doesn't seem to be a way to access the scoped services there,
// so we are using a service locator here. If someone could let me know how
// that is done in Startup.Configure() of .NET Core 2.0, please leave a comment.
public IDictionary<string, int> GetPageToIdMap(IServiceProvider serviceProvider)
{
using (var dbContext = serviceProvider.GetRequiredService<ApplicationDbContext>())
{
// Query the categories so we can build all of the URLs client side
var categories = dbContext.Categories.ToList();
var scratch = new StringBuilder();
return (from category in categories
select new KeyValuePair<string, int>(
GetUrl(category, categories, scratch),
category.CategoryId)
).ToDictionary(pair => pair.Key, pair => pair.Value);
}
}
private string GetUrl(Category category, IEnumerable<Category> categories, StringBuilder result)
{
result.Clear().Append(category.Slug);
while ((category = categories.FirstOrDefault(c => c.CategoryId == category.ParentCategoryId)) != null)
{
result.Insert(0, string.Concat(category.Slug, "/"));
}
return result.ToString();
}
}
类别控制器
除了我们此时根本不需要处理 URL 或 slug 之外,控制器中没有发生任何特殊情况。我们只需接受映射到记录主键的 id
参数,然后您就知道该怎么做了...
public class CategoryController : Controller
{
public IActionResult Index(int id)
{
// Lookup category based on id...
return View();
}
}
用法
我们在Startup.cs
中配置如下:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddMvc();
services.AddSingleton<CategoryCachedRouteDataProvider>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.Routes.Add(
new CachedRoute<int>(
controller: "Category",
action: "Index",
dataProvider: app.ApplicationServices.GetRequiredService<CategoryCachedRouteDataProvider>(),
cache: app.ApplicationServices.GetRequiredService<IMemoryCache>(),
target: routes.DefaultHandler)
{
CacheTimeoutInSeconds = 900
});
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
请注意,CachedRoute<TPrimaryKey>
可以重复用于为其他 table 创建额外的路由。因此,如果您愿意,还可以通过在类别 table 上使用联接并使用类似的方法构建 URL 来使您的产品 URL 像 guitars/acoustic-guitars/some-fancy-acoustic-guitar
.
可以使用 Tag Helpers 或任何其他基于 UrlHelper
的方法生成 URL 并将其添加到 UI。例如:
<a asp-area="" asp-controller="Category" asp-action="Index" asp-route-id="12">Effects</a>
生成为
<a href="/amps-and-effects/effects">Effects</a>
您当然可以使用模型的主键生成 URL 和 link 的文本 - 使用具有主键和名字.
您唯一需要做的就是为 link 显示创建层次结构。但这不在路由的范围内。
请注意,在路由中根本没有层次结构的概念 - 它只是在每个请求中从上到下匹配的路由列表。
If the routing template is set up to support that, how to retrieve the leaf category? For example, from the URL /guitars/acoustic-guitars, I would like to retrieve acoustic-guitars as the leaf category, and able to get all products under that acoustic-guitars category. Note: I don't want manual parsing on the URL. Ideally it would be the best if the lead category is binded through model binding.
不清楚为什么需要 "leaf category",因为这与传入或传出路由无关,也不需要查找数据库数据。同样,主键是根据路由生成整个 URL 所需的全部内容,它应该是查看所有产品所需的全部内容。但是如果你真的需要访问它,你可以在你的控制器中查找它。
自定义
您可能需要根据您的具体要求更改缓存策略。
- 您可能希望在 RAM 中使用固定最大 link 数量的 LRU 缓存策略
- 您可能希望跟踪 URL 被点击的频率,并将最常访问的 URL 移至列表顶部
- 您可能希望在路由和更新操作方法之间共享缓存,这样当 URLs 在数据库中成功更新时,它们也会同时在缓存中更新 "real-time" URLs
- 您可能希望单独缓存每个 URL 并一次查找一个,而不是一次缓存整个列表