简体   繁体   English

MVC4控制器/路由不起作用

[英]MVC4 controller/route not working

my VisitsController: 我的VisitsController:

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

Here is my route config 这是我的路线配置

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

What would the custom route be (and force it to be an integer)? 自定义路由将是什么(并将其强制为整数)?

You need to add a new route for that to work. 您需要添加新的路由才能正常工作。

Currently, this will work: 目前,这将起作用:

/Visits/Index/123

I believe you need to add the following: 我相信您需要添加以下内容:

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

This is assuming you have a modelbinder for Visits already. 假设您已经有一个用于访问的模型绑定器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM