Search code examples
asp.net-mvcattributerouting

How to use attribute routing in one action?


I need to routing to one action by attribute routing in mvc5 . My action :

  [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){}

Query string is :

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

I need to access to this action by this url :

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

I use this code in 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" }
            );

When Run app get error :

How to fix this?


Solution

  • I solve my problem :

    [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)