How do i have optional parameter but still validate them in asp.net mvc routing ?

Posted by ooo on Stack Overflow See other posts from Stack Overflow or by ooo
Published on 2010-12-31T14:25:36Z Indexed on 2010/12/31 20:54 UTC
Read the original article Hit count: 204

Filed under:
|

I have this route that i just added

        routes.MapRoute(
            "MyRoute",
            "MyController/{action}/{orgId}/{startDate}/{endDate}",
            new
                {
                    controller = "MyController",
                    action = "MyAction",
                    orgId = 0,
                    startDate = DateTime.Today.AddMonths(-1),
                    endDate = DateTime.Today
                },
            new
            {
                action = new FromValuesListConstraint(new string[] { "MyAction", "MyActionEx" }),
                orgId = new IntegerRouteConstraint(),
                startDate = new DateTimeRouteConstraint(),
                endDate = new DateTimeRouteConstraint()
            }

when i put in this url, it resolves down to the default route (controller, action,id) and the above rout does not catch this url:

http://localhost:1713/MyController/MyAction/16

But this below works fine.

http://localhost:1713/MyController/MyAction/16/11-May-10/11-May-10

my question is that i thought both would work as i am giving default values to the startDate and enddate fields

i tested this using the RouteDebugger and this route turned up false

how can i have these last two parameter as optional but still have the validation ?

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about url-routing