伙计,我的asp.net mvc网站上有一个页面。 路线配置:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{index}", // URL with parameters
new { controller = "Home", action = "Detail", index = "" } // Parameter defaults
);
}
控制器代码:
public ActionResult Detail(string index)
{
string[,] List = new string[,] { {"1", "first item"}, {"3", "middle item"}, {"5", "last item"}};
ViewData["Message"] = "no results.";
if (!string.IsNullOrEmpty(index))
{
for (int i = 0; i <= List.GetUpperBound(0); i++)
{
if (List[i, 0] == index)
{
ViewData["Message"] = List[i, 1];
}
}
}
return View();
}
我希望用户使用参数5访问http://www.domain.com/5重定向操作详细信息。
如何支持它?
认为
答案 0 :(得分:6)
尝试在默认路线之前定义以下路线:
routes.MapRoute(
"Custom",
"{index}",
new { controller = "Home", action = "Detail", index = "" }
);