自定义路由匹配和生成
来源:互联网 发布:c 显示数据库数据控件 编辑:程序博客网 时间:2024/06/18 05:16
前言
《ASP.NET Core 中的SEO优化(1):中间件实现服务端静态化缓存》
《ASP.NET Core 中的SEO优化(2):中间件中渲染Razor视图》
背景
栏目的列表 ->
/{父栏目名}/{子栏目名}-{页码}/
文章详情页 ->
/{栏目名}/{文章名}.html
标签页 ->
/{标签名}
app.UseMvc(routes =>
{
routes.MapRoute(
name: "article_list",
template: "{parentCategory}/{category}-{page}/",
defaults: new { controller = "Article", action = "Index" });
routes.MapRoute(
name: "article_detail",
template: "{category}/{article}.html",
defaults: new { controller = "Article", action = "Detail" });
routes.MapRoute(
name: "tags",
template: "{tag}/",
defaults: new { controller = "Article", action = "Tag" });
});
原理
namespace Microsoft.AspNetCore.Routing
{
public interface IRouter
{
Task RouteAsync(RouteContext context);
VirtualPathData GetVirtualPath(VirtualPathContext context);
}
}
实现
RouteAsync
public async Task RouteAsync(RouteContext context)
{
var requestedUrl = context.HttpContext.Request.Path.Value.TrimStart('/').ToLower();
var split = requestedUrl.Split('/');
if (secoend != null && secoend.EndsWith(".html") && split.Length == 2)
{
var title = secoend.Replace(".html", "");
context.RouteData.Values["controller"] = "Article";
context.RouteData.Values["action"] = "Detail";
context.RouteData.Values["category"] = first;
context.RouteData.Values["title"] = title;
}
//...对请求路径进行一系列的判断
//最后注入`MvcRouteHandler`示例执行`RouteAsync`方法,表示匹配成功
await context.HttpContext.RequestServices.GetService<MvcRouteHandler>().RouteAsync(context);
}
GetVirtualPath
public VirtualPathData GetVirtualPath(VirtualPathContext context)
{
var path = string.Empty;
var hasController = context.Values.TryGetValue("controller", out var controller);
var hasAction = context.Values.TryGetValue("action", out var action);
var hasCategory = context.Values.TryGetValue("category", out var category);
var hasTitle = context.Values.TryGetValue("title", out var title);
if (hasController && hasAction && hasCategory && hasTitle)
{
path = $"/{category/{title}.html";
}
return path != string.Empty ? new VirtualPathData(this, path) : null;
}
IRouter的设置生效
app.UseMvc(routes =>
{
//添加 自定义路由匹配与url生成组件
routes.Routes.Add(new RouteProvider());
});
相关小技巧
public static class UrlHelperExtensions
{
public static string AbsoluteAction(
this IUrlHelper helper,
string actionName,
string controllerName,
object routeValues = null)
{
string scheme = helper.ActionContext.HttpContext.Request.Scheme;
return helper.Action(actionName, controllerName, routeValues, scheme);
}
public static string AbsoluteContent(
this IUrlHelper helper,
string contentPath)
{
return new Uri(helper.ActionContext.HttpContext.Request.GetUri(), helper.Content(contentPath)).ToString();
}
public static string AbsoluteRouteUrl(
this IUrlHelper helper,
string routeName,
object routeValues = null)
{
string scheme = helper.ActionContext.HttpContext.Request.Scheme;
return helper.RouteUrl(routeName, routeValues, scheme);
}
}
总结
原文:https://yangshunjie.com/A-Middleware-Implement-For-Customized-Routing-In-AspNetCore.html
.NET社区新闻,深度好文,欢迎访问公众号文章汇总 http://www.csharpkit.com
- 自定义路由匹配和生成
- 路由和路由表生成算法
- ip4和ipv6的路由匹配
- 网络层核心:路由和路由生成算法
- HttpHandler路由匹配规则
- 路由匹配顺序
- react-router 路由匹配
- 路由最长匹配原则
- 生成随机密码和邮箱、手机匹配
- mvc3 路由学习 自定义路由
- vue动态路由匹配,路由传参
- MVC的Partial ,自定义路由,认证和授权
- 路由和路由算法
- thinkphp5.0学习(四):入口文件、路由模式、路由设置和url生成
- 路由掩码最长匹配原则
- MVC3路由设置匹配小结
- 路由查找最长前缀匹配
- 路由查找最长前缀匹配
- HDU3829-最大独立子集
- 基于python实现日志收集
- python函数-高阶函数和匿名函数
- 将jTessBoxEditor训练的字库合并
- 关于pl/sql远程连接oracle
- 自定义路由匹配和生成
- QT控件大全一AnalogWidgets
- 将string字符串转化为16进制字符串
- 反转链表
- VS调试Mex代码
- 关于java.lang.invoke包的解释
- 富文本wangEditor
- Pytorch 0.3发布:实现多方面提速,增加对ONNX支持 | 快讯
- 比AlphaGo Zero更强的AlphaZero来了!8小时解决一切棋类!| 快讯