MVC4控制器/路由无法正常工作

时间:2012-10-17 19:48:12

标签: c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing

我的VisitsController:

public ActionResult Index(Visits visits, int? id)
{
    ....
    return View(v);
}

这是我的路线配置

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", 
                        id = UrlParameter.Optional }
    );
}


Works: http://localhost:49225/Visits
Does not work: http://localhost:49225/Visits/123

自定义路由是什么(并强制它为整数)?

1 个答案:

答案 0 :(得分:1)

您需要添加新的路线才能发挥作用。

目前,这将有效:

/Visits/Index/123

我相信你需要添加以下内容:

routes.MapRoute(
    name: "VisitsDefault",
    url: "Visits/{id}",
    defaults: new { controller = "Visits", action = "Index", 
                    id = UrlParameter.Optional }
);

假设您已经有Visits的模型绑定器。