我有一个产品类别表来表示分层类别结构,这是数据库中典型的Parent-Child
关系表。
以吉他中心的数据填写:
如果您将其呈现为包含<ul>
和<li>
的网页:
蓝色文本是我想要生成的网址。对于任何给定的类别,链接包括其slug及其父母&#39;蛞蝓。
请注意,我列出的示例只有2个父子级别。理论上,通过自我指涉结构,任何孩子都可以拥有无限的父母。
categories/guitars/acoustic-guitars
中,我想检索acoustic-guitars
作为叶子类别,并且能够获得该acoustic-guitars
类别下的所有产品。注意:我不想在URL上进行手动解析。理想情况下,如果通过模型绑定绑定主要类别,那将是最好的。 答案 0 :(得分:0)
如何设置路由模板来实现这一目标?
你做不到。但是您可以降低到较低级别并为CMS样式的路由管理进行数据驱动的IRouter
实现。
CachedRoute<TPrimaryKey>
这是一个跟踪和缓存主键到URL的1-1映射的示例。它是一个泛型类,我测试过无论主键是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));
}
}
在这里,我们从数据库中查找类别,并递归地将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>
来为其他表创建其他路由。因此,如果您愿意,还可以使用类别表上的联接并使用类似的方法构建网址,从而使您的产品网址如guitars/acoustic-guitars/some-fancy-acoustic-guitar
。
可以使用Tag Helpers或任何其他基于UrlHelper
的方法生成网址并将其添加到用户界面。例如:
<a asp-area="" asp-controller="Category" asp-action="Index" asp-route-id="12">Effects</a>
生成为
<a href="/amps-and-effects/effects">Effects</a>
当然,您可以使用模型的主键生成链接的URL和文本 - 使用具有主键和名称的模型,这一切都是自动且直接的。
您需要做的唯一额外事情是为链接显示创建层次结构。但这超出了路由的范围。
请注意,路由中根本没有层次结构的概念 - 它只是每个请求从上到下匹配的路由列表。
如果路由模板设置为支持,那么如何检索叶子类别?例如,从URL /吉他/声学吉他,我想检索声学吉他作为叶子类别,并能够获得该声学吉他类别下的所有产品。注意:我不希望在URL上进行手动解析。理想情况下,如果通过模型绑定绑定主要类别,那将是最好的。
目前还不清楚为什么你需要一个“叶子类别”,因为它与传入或传出路由无关,也不需要查找数据库数据。同样,主键是您根据路由生成整个URL所需的全部内容,它应该是查看所有产品所需的全部内容。但是如果你真的需要访问它,你可以在控制器中查找它。
您可能需要根据具体要求更改缓存策略。