自定义路由匹配和生成
前言
-
《ASP.NET Core 中的SEO優(yōu)化(1):中間件實現(xiàn)服務端靜態(tài)化緩存》
-
《ASP.NET Core 中的SEO優(yōu)化(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);
? ?}
}
實現(xiàn)
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`示例執(zhí)行`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社區(qū)新聞,深度好文,歡迎訪問公眾號文章匯總 http://www.csharpkit.com
總結
以上是生活随笔為你收集整理的自定义路由匹配和生成的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 改造独立部署(SCD)模式下.NET C
- 下一篇: Net Core下多种ORM框架特性及性