-1

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?

hmahdavi
  • 2,250
  • 3
  • 38
  • 90

2 Answers2

1

Remove the spaces from the route template and string is the default so no need to include it.

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

Reference Attribute Routing in ASP.NET MVC 5

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • I delete spaces from route and add another params to route but don't solved – hmahdavi Nov 29 '17 at 05:29
  • I change such as [Route("bus/{DepProvince:int}/{From:int}/{DesProvince:int}/{To:int}/{DepartureDate}/{IsForeign}")] and solved – hmahdavi Nov 29 '17 at 05:49
  • For {IsForeign:bool} get error . How to specific bool type for attributes? – hmahdavi Nov 29 '17 at 05:50
  • @programmer138200 for your answer on how to use boolean, [check this](https://stackoverflow.com/questions/33412425/map-url-text-to-boolean-parameter-with-mvc-attribute-routing), the first answer of using enums. – inthevortex Nov 29 '17 at 06:13
  • I change type of boolean param to int and Now I have another problem that when go to previous urls's such as /bus/index or /bus get error that The resource cannot be found. – hmahdavi Nov 29 '17 at 09:50
1

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)
hmahdavi
  • 2,250
  • 3
  • 38
  • 90