Route matching question

I’m rather new at this so this may be a simple answer. Given the controller class below:

    public class DataManagerController : Controller
    {

        public ActionResult ShowHistory(
                                    int showID,
                                    string venueIDs,
                                    string bookerID,
                                    DateTime dateFrom,
                                    DateTime dateTo)
        {
            return Json(_ShowHistoryRepository.GetData(showID, venueIDs, bookerID, dateFrom, dateTo), JsonRequestBehavior.AllowGet);
        }

}

I’m displaying a grid on a view where the datasource is set to "DataManager/ShowHistory". The grid fills with data. What I don’t understand is how. I commented out all but the base MapRoute in Global.cs as shown here:

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            //routes.MapRoute(
            //    name: "Default1",
            //    url: "DataManager/ShowHistory/{showID}/{venueIDs}/{bookerID}/{dateFrom}/{dateTo}",
            //    defaults: new
            //    {
            //        controller = "DataManager",
            //        action = "GetShowHistory",
            //        showID = string.Empty,
            //        venueIDs = string.Empty,
            //        bookerID = string.Empty,
            //        dateFrom = DateTime.Now.AddYears(-6),
            //        dateTo = DateTime.Now
            //    }
            //);

            //routes.MapHttpRoute(
            //    name: "DefaultApi",
            //    routeTemplate: "api/{controller}/{id}",
            //    defaults: new { id = RouteParameter.Optional }
            //);

            //routes.MapRoute(
            //    name: "Default2",
            //    url: "{controller}/{action}/{id}/{groupType}",
            //    defaults: new { controller = "Home", action = "Index", id = 0, groupType = 0 }
            //);

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

Yet, when I run the page the app finds the controller/method, passes the parameters, and executes just fine. What I don’t understand is how that could possibly be when I only have one route mapping that doesn’t even match the pattern. However, if I add parameters
to the ShowHistory method of the DataManagerController, the ShowHistory method no longer executes, I’m guessing. because it cannot be found..

What am I not understanding?

Thanks

Carl

recompile. ANd clear browser cache.

I tried this multiple times but to no avail. Is my understanding correct that this shouldn’t work as I’ve explained it?

Thanks

Carl

How do you call the action?

Hi CarlGanz,

Thanks for your post.

CarlGanz

However, if I add parameters to the ShowHistory method of the DataManagerController, the ShowHistory method no longer executes,

Did you get any errors?How did you call the action?

I think you can read this article:

http://www.codeproject.com/Articles/641783/Customizing-Routes-in-ASP-NET-MVC

Best Regards,

Eileen

it works because of the default route is not commented…

In default route it is mentioned the parameters are optional so it is not necessary to match showhistory. 

change your showID parameter to ID and other parameters as optional then with below url it should work

DataManager/showhisotry/1

for more on asp.net mvc routing check below article

http://dotnetmentors.com/mvc/explain-asp-net-mvc-routing-with-example.aspx

It doesn’t throw an error. Rather, it just fails to find the controller method, I suppose, because the signature no no longer matches what its expecting. I’m just not sure where its expecting this:

        public ActionResult ShowHistory(
                                    int showID,
                                    string venueIDs,
                                    string bookerID,
                                    DateTime dateFrom,
                                    DateTime dateTo)

and not this

        public ActionResult ShowHistory(

                                    int holdID,
                                    int showID,
                                    string venueIDs,
                                    string bookerID,
                                    DateTime dateFrom,
                                    DateTime dateTo)

I read the CodeProject article you suggested when I started this project and thought I was doing it the way it explained.

I’ve narrowed down to a controller method like this:

        public ActionResult ShowHistory(int holdID,
                                    DateTime eventDate,
                                    int showID,
                                    string venueIDs,
                                    string eventTypeIDs,
                                    string bookerID,
                                    DateTime dateFrom,
                                    DateTime dateTo)
        {
            return Json(_ShowHistoryRepository.GetData(holdID, eventDate, showID, venueIDs, eventTypeIDs, bookerID, dateFrom, dateTo), JsonRequestBehavior.AllowGet);
        }

A route in Global.asax like this:

            routes.MapRoute(
                name: "Default1",
                url: "{controller}/{action}/{holdID}/{eventDate}/{showID}/{venueIDs}/{eventTypeIDs}/{bookerID}/{dateFrom}/{dateTo}"
            );

invoked from JavaScript by this code:

        $(‘#grid’).data(‘kendoGrid’).dataSource.read(
            {
                "HoldID": holdID,
                "EventDate": eventDate,
                "ShowID": artist,
                "VenueIDs": venues,
                "EventTypeIDs": eventTypes,
                "BookerID": booker,
                "DateFrom": dateFrom,
                "DateTo": dateTo
            });

and nothing happens.

Thanks

Carl

Leave a Reply