首页 新闻 会员 周边

ASP.NET Core: 如何通过手动档实现 route template 匹配字符串

0
悬赏园豆:30 [已解决问题] 解决于 2024-01-04 22:15

实际需求场景是通过博文 url 获取对应的博文,现在已知多个 route template

public static class RouteTemplates
{
    public const string BlogPostUrl = "/{blogApp}/{postType}/{id:int}/{**slug}";  
    public const string BlogPostUrlWithExt = "/{blogApp}/{postType}/{idOrSlug}.html";  
}

需要根据传入的 url 字符串判断匹配的是哪个路由模板,请问如何实现?

dudu的主页 dudu | 高人七级 | 园豆:30994
提问于:2024-01-04 18:08
< >
分享
最佳答案
0

在 stackoverflow 上找到了解决方法,详见 Parse to object with a route like ASP.NET MVC routing

using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing.Template;

class Program
{
    static void Main()
    {
        var routeTemplates = new[]
        {
            "/{blogApp}/{postType}/{id:int}/{**slug}",
            "/{blogApp}/{postType}/{idOrSlug}.html"
        };

        var urlPath = "/cmt/p/17940002";

        foreach (string template in routeTemplates)
        {
            var routeTemplate = TemplateParser.Parse(template);
            var values = new RouteValueDictionary();
            var matcher = new TemplateMatcher(routeTemplate, values);
            if (matcher.TryMatch(urlPath, values))
            {
                foreach (var item in values)
                {
                    Console.WriteLine("{0}: {1}", item.Key, item.Value);
                }
            }
        }
    }
}

输出是

blogApp: cmt
postType: p
id: 17940002
slug:
dudu | 高人七级 |园豆:30994 | 2024-01-04 22:14
dudu | 园豆:30994 (高人七级) | 2024-01-06 11:47
其他回答(1)
0

在ASP.NET Core中,可以使用TemplateMatcher类来手动匹配路由模板。以下是一个简单的示例代码,演示如何通过手动匹配字符串来确定匹配的路由模板:

csharp
Copy code
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Primitives;
using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
string url = "/myBlog/normal/123/some-slug";

    // Define route templates
    string blogPostUrlTemplate = "/{blogApp}/{postType}/{id:int}/{**slug}";
    string blogPostUrlWithExtTemplate = "/{blogApp}/{postType}/{idOrSlug}.html";

    // Match the URL against route templates
    string matchedTemplate = MatchRouteTemplate(url, blogPostUrlTemplate, blogPostUrlWithExtTemplate);

    if (matchedTemplate != null)
    {
        Console.WriteLine($"Matched Route Template: {matchedTemplate}");
    }
    else
    {
        Console.WriteLine("No matching route template found.");
    }
}

static string MatchRouteTemplate(string url, params string[] routeTemplates)
{
    foreach (string template in routeTemplates)
    {
        RouteTemplate parsedTemplate = TemplateParser.Parse(template);
        RouteValueDictionary values = new RouteValueDictionary();

        if (parsedTemplate.TryMatch(url, values))
        {
            // Matched template
            return template;
        }
    }

    // No matching template found
    return null;
}

}
上述示例中,MatchRouteTemplate方法通过遍历给定的路由模板,使用TemplateParser.Parse方法将模板字符串解析为RouteTemplate对象,然后使用TryMatch方法匹配传入的URL。如果找到匹配的模板,则返回该模板字符串。

请注意,这只是一个简单的示例,实际使用中可能需要考虑更多的细节和边界情况。TemplateMatcher和RouteTemplate类提供了更灵活的路由匹配功能,你可以根据具体需求进行进一步的扩展和调整。

收获园豆:30
Technologyforgood | 园豆:5681 (大侠五级) | 2024-01-04 20:23

博问支持 markdown 语法,建议改进一下排版

支持(0) 反对(0) dudu | 园豆:30994 (高人七级) | 2024-01-04 21:54

上面的代码有2个问题:

  • 少了 using Microsoft.AspNetCore.Routing.Template;
  • 没有 parsedTemplate.TryMatch() 方法
支持(0) 反对(0) dudu | 园豆:30994 (高人七级) | 2024-01-04 21:57

TryMatch 方法是 TemplateMatcher 中的

支持(0) 反对(0) dudu | 园豆:30994 (高人七级) | 2024-01-04 22:11
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册