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?

Answers


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.


Need Your Help

How to refer to a method?

c# reflection

I'm trying to create a router for a web application (yes, I know solutions already exist).

linq check if matches null or selected item

c# asp.net sql-server linq

I am checking to see if items already match whats in my MSSQL DB. I am using LINQ to update records. I would like to know how i can check if an item is equal to d_0_2 or if its equal to null/empty....

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.