开发者

ASP.net MVC custom route

开发者 https://www.devze.com 2023-01-12 11:55 出处:网络
guy, i have a page in my asp.net mvc website. the Route configuration: public static void RegisterRoutes(RouteCollection routes)

guy, i have a page in my asp.net mvc website. the Route configuration:

        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
        );
    }

the Controller code:

        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();
    }

and i want user to visit http://www.domain.com/5 redirect Action Detail with parameter 5.

how to support it?

thinks.


Try defining the following route before the default route:

routes.MapRoute(
    "Custom",
    "{index}",
    new { controller = "Home", action = "Detail", index = "" }
);
0

精彩评论

暂无评论...
验证码 换一张
取 消