【问题标题】:How to use Route Constraint and Attribute Routing together?如何同时使用路由约束和属性路由?
【发布时间】:2017-11-30 05:50:37
【问题描述】:

我在我的网站中使用了路由约束,现在我需要使用属性路由。

路由约束类:

public class BusConstraint : IRouteConstraint
    {
        private RouteDB routeDb = new RouteDB();


        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            if (values[parameterName] != null)
            {
                var permalink = values[parameterName].ToString();

                try
                {
                    List<AdakDbConnect.RouteTable> routeTables;
                    if (HttpContext.Current.Cache["RouteTables"] == null)
                    {
                        routeTables = routeDb.GetRouteTables(AdakDbDll.Username, AdakDbDll.Password);
                        HttpContext.Current.Cache.Add("RouteTables", routeTables, null, DateTime.Now.AddMinutes(45), TimeSpan.Zero, CacheItemPriority.Normal, null);
                    }
                    else
                    {
                        routeTables = HttpContext.Current.Cache["RouteTables"] as List<AdakDbConnect.RouteTable>;
                    }
                    RouteTable Route = routeTables?.FirstOrDefault(p => p.Link == permalink && p.Controlller == "Bus");
                    if (Route != null)
                    {
                        return true;
                    }
                }
                catch (System.Exception)
                {

                }


                return false;
            }
            return false;
        }
    }

对于属性路由,使用上面的代码:

[Route("Bus")]
[Route("Bus/index")]
[Route("Bus/{From:int}/{To:int}/{Date:int}/{IsForeign:int:range(0,1)}/{Title}")]

曾经的 Attribute Routing 和 Route Constraint 目前单独工作。但是当我一起使用时,例如这种情况下,属性路由只起作用,而路由约束不起作用。

路由配置:

    public static void RegisterRoutes(RouteCollection routes)
            {
                routes.MapMvcAttributeRoutes();

                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

                //---------------------BusRoute---------------------------
                routes.MapRoute(
                name: "BusRoute",
                url: "{*permalink}",
                defaults: new { controller = "Bus", action = "Index" },
                constraints: new { permalink = new BusConstraint() },
                namespaces: new[] { "TravelEnterProject.Controllers" }
                );
}

如何同时使用路由约束和属性路由?

【问题讨论】:

  • 您需要创建一个自定义属性 - 请参阅 this article 以获取示例
  • 我的 URL 之一是“ /Bus-Ticket ” 现在如果我删除当前工作的属性路由或如果添加属性路由,例如 [Route("Bus-Ticket")] 当前工作。现在如何创建自定义属性路由?能发个代码吗?

标签: asp.net-mvc routing attributerouting route-constraint


【解决方案1】:

路由将按照您在 RouteConfig 文件中定义的顺序工作。

根据您的 RouteConfig.cs 文件

你定义了 routes.MapMvcAttributeRoutes(); --> 用于属性路由 然后你定义了常规路由。

所以如果你在控制器中同时使用了路由,那么只有属性路由会起作用。

如果你想按照常规路由工作,那么改变def的顺序。

首先定义常规路由routes.MapRoute() 然后是routes.MapMvcAttributeRoutes();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    • 2015-08-11
    • 1970-01-01
    • 2011-04-20
    相关资源
    最近更新 更多