忽略 ASP.NET MVC Core 路由中的第一段

Tim*_*Tim 3 asp.net-mvc routes url-routing asp.net-mvc-routing .net-core

我正在寻找能够匹配以下路线的路线定义:

  • /segment/xxxx/def
  • /segment/.../xxxx/def
  • /segment/that/can/span/xxxx/def

并能够xxxx使用参数 `def 运行操作。

但这种路线是不允许的:

[Route("/{*segment}/xxx/{myparam}")]
Run Code Online (Sandbox Code Playgroud)

如何做呢?

Nig*_*888 5

您可以将自定义IRouter与正则表达式结合使用来执行高级 URL 匹配,如下所示。

public class EndsWithRoute : IRouter
{
    private readonly Regex urlPattern;
    private readonly string controllerName;
    private readonly string actionName;
    private readonly string parameterName;
    private readonly IRouter handler;

    public EndsWithRoute(string controllerName, string actionName, string parameterName, IRouter handler)
    {
        if (string.IsNullOrWhiteSpace(controllerName))
            throw new ArgumentException($"'{nameof(controllerName)}' is required.");
        if (string.IsNullOrWhiteSpace(actionName))
            throw new ArgumentException($"'{nameof(actionName)}' is required.");
        if (string.IsNullOrWhiteSpace(parameterName))
            throw new ArgumentException($"'{nameof(parameterName)}' is required.");
        this.controllerName = controllerName;
        this.actionName = actionName;
        this.parameterName = parameterName;
        this.handler = handler ??
            throw new ArgumentNullException(nameof(handler));
        this.urlPattern = new Regex($"{actionName}/[^/]+/?$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
    }

    public VirtualPathData GetVirtualPath(VirtualPathContext context)
    {
        var controller = context.Values.GetValueOrDefault("controller") as string;
        var action = context.Values.GetValueOrDefault("action") as string;
        var param = context.Values.GetValueOrDefault(parameterName) as string;

        if (controller == controllerName && action == actionName && !string.IsNullOrEmpty(param))
        {
            return new VirtualPathData(this, $"{actionName}/{param}".ToLowerInvariant());
        }
        return null;
    }

    public async Task RouteAsync(RouteContext context)
    {
        var path = context.HttpContext.Request.Path.ToString();

        // Check if the URL pattern matches
        if (!urlPattern.IsMatch(path, 1))
            return;

        // Get the value of the last segment
        var param = path.Split('/').Last();

        //Invoke MVC controller/action
        var routeData = context.RouteData;

        routeData.Values["controller"] = controllerName;
        routeData.Values["action"] = actionName;
        // Putting the myParam value into route values makes it
        // available to the model binder and to action method parameters.
        routeData.Values[parameterName] = param;

        await handler.RouteAsync(context);
    }
}
Run Code Online (Sandbox Code Playgroud)

用法

app.UseMvc(routes =>
{
    routes.Routes.Add(new EndsWithRoute(
        controllerName: "Home", 
        actionName: "About", 
        parameterName: "myParam", 
        handler: routes.DefaultHandler));

    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
});
Run Code Online (Sandbox Code Playgroud)

该路由被参数化,以允许您传入与所调用的操作方法相对应的控制器、操作和参数名称。

public class HomeController : Controller
{
    public IActionResult About(string myParam)
    {
        ViewData["Message"] = "Your application description page.";

        return View();
    }
}
Run Code Online (Sandbox Code Playgroud)

需要做更多的工作才能使其匹配任何操作方法名称,并能够使用该操作方法名称再次构建 URL。但这一路线将允许您通过多次注册来添加其他操作名称。

注意:出于 SEO 目的,将相同内容放在多个 URL 上通常不被认为是一个好的做法。如果您这样做,建议使用规范标签来告知搜索引擎哪个 URL 是权威的

请参阅此内容以在 ASP.NET MVC 中完成相同的操作(在 ASP.NET Core 之前)