我是asp.net mvc的初学者。
我有2个控制器:
HomeController操作:索引,关于
我需要的网址:
index action url: mydomain
about action url: mydomain/about
其他控制器操作:索引
index action url: mydomain/other
MyCode无效:
routes.MapRoute(
"Other",
"{controller}/{action}/{id}",
new { controller = "Other", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
由于
答案 0 :(得分:2)
对于您的家庭控制器,点击
index action url: mydomain
你需要
routes.MapRoute(
name: "Home",
url: "",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
点击
about action url: mydomain/about
你需要
routes.MapRoute(
name: "Home",
url: "about",
defaults: new { controller = "Home", action = "About", id = UrlParameter.Optional }
);
对于您的其他控制器,点击
index action url: mydomain/other
你需要
routes.MapRoute(
name: "Other",
url: "other",
defaults: new { controller = "Other", action = "Index", id = UrlParameter.Optional }
);
请注意,在所有情况下,“name”参数并不重要。
答案 1 :(得分:-1)
我认为您在{controller}
路由规范中错过了Default
。
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);