Jose3d
Jose3d

Reputation: 9277

RouteValueDictionary initialization and MapRoute issue

Are these two pieces of codes the same?

RouteValueDictionary dic=new RouteValueDictionary();
dic.Add("controller", "Home");
dic.Add("action", "Index");
RouteTable.Routes.MapRoute("Test", "Test/Something", dic);

and

 RouteTable.Routes.MapRoute("Test", "Test/Something", new{controller="Home", action="Index"});

I am not getting the same route in the route table. When I use the first option, the keys "controller" and "action" arent in RouteTable.Routes[0].Defaults.Keys but are added on the RouteTable.Routes[0].Defaults.Values

Do you know what I am doing wrong in the first option?

Upvotes: 3

Views: 1983

Answers (1)

Behnam Esmaili
Behnam Esmaili

Reputation: 5967

you can pass object of any type for third parameter but the passed object must have included keys defined in you'r url pattern as it's properties.for example

 public class test
    {
        public string controller { get; set; }
        public string action { get; set; }
        public string id { get; set; }
    } 

     routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}", 
             new test { action = "Index", controller = "Home", id = "" } 
        );

in this case you'r object must include controller,action,id properties.

Upvotes: 3

Related Questions