如何在一个动作中使用属性路由?

How to use attribute routing in one action?

我需要通过 mvc5 中的属性路由路由到一项操作。 我的行动:

  [Route("bus/{DepProvince}{From}/{DesProvince}-{To}/{DepartureDate}/{IsForeign}")]
        public ActionResult Index(int? DepProvince, int? From/*City*/, int? DesProvince, int? To/*City*/, string DepartureDate, bool? IsForeign){}

查询字符串是:

/Bus?DepProvince=11000000&From=11321006&RetProvince  =31000000&To=31310000&DepartureDate=1396%2F09%2F07  &IsForeign=False

我需要通过此 url 访问此操作:

/Bus/11000000-11321006/31000000-31310000/13960907/0

我在 Routconfig.cs 中使用此代码:

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" }
            );

当 运行 应用出现错误时:

如何解决这个问题?

从路由模板中删除空格,字符串是默认值,因此无需包含它。

[Route("bus/{From:int}-{To:int}/{DepartureDate}/{IsForeign:bool}")]

引用Attribute Routing in ASP.NET MVC 5

我解决了我的问题:

[Route("bus")]
[Route("bus/index")]

    [Route("bus/{From:int}/{To:int}/{Date:int}/{IsForeign:int:range(0,1)}/{Title}")]
                public ActionResult Index(int? From, int? To, int? Date, int? IsForeign)