这两段代码是一样的吗?
RouteValueDictionary dic=new RouteValueDictionary();
dic.Add("controller", "Home");
dic.Add("action", "Index");
RouteTable.Routes.MapRoute("Test", "Test/Something", dic);
和
RouteTable.Routes.MapRoute("Test", "Test/Something", new{controller="Home", action="Index"});
我在路由表中没有得到相同的路由。当我使用第一个选项时,键“controller”和“action”不在 RouteTable.Routes[0].Defaults.Keys
中,而是添加到 RouteTable.Routes[0] 中。默认值。值
你知道我在第一个选项中做错了什么吗?
最佳答案
您可以为第三个参数传递任何类型的对象,但传递的对象必须包含在您的 url 模式中定义的键,因为它是属性。例如
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 = "" }
);
在这种情况下,您的对象必须包含 controller
、action
、id
属性。
关于asp.net-mvc - RouteValueDictionary 初始化和 MapRoute 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13994864/