Search code examples
asp.net-mvc-4routesmaproute

Correct Route Patterns in MVC4


I Have a calendar at www.server.com/events/calendar. My event query string looks like www.server.com/events/calendar/seofriendly-event-query-string. But users can select the events by year and months using drop down lists, so mu query becomes www.server.com/events/calendar/2013 or even www.server.com/events/calendar/2013/12. So the problem is when I click the www.server.com/events/calendar/seofriendly-event-query-string I got www.server.com/events/calendar. How to arrange my routes to make them understand what I need to show: list or event?


Solution

  • well I would add a custom route like this:

    routes.MapRoute(
        "NewRoute", // Route name
        "{controller}/{action}/{id}/{another_id}", // URL with parameters
        new { controller = "Events", action = "Calendar", id = UrlParameter.Optional, another_id = UrlParameter.Optional } // Parameter defaults
    );
    

    Your controller would then have an action method like this:

    public ActionResult MyAction(string id, string another_id)
    {
        // in the question you mentioned that a a valid list querystring would contain
    // multiple integer parameters, and an event querystring would include a 
    //seo friendly string
        int para;
         if (int.TryParse(id,out para))
            {
            // show list view
            }
         else
            {
            //show event view
            }
     }
    

    you just need to recieve the parameters and run some kind of check to determine if you are going to show an event or a list.