带有参数的ASP MVC路由在url的开头

时间:2014-07-15 11:51:14

标签: asp.net asp.net-mvc asp.net-mvc-4

Hello默认ASP MVC路由如下:

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

但是可以以下一种方式使用它而不是可选ID:

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

1 个答案:

答案 0 :(得分:2)

是的,这是可能的。

刚试过:

RoutesRegistration:

routes.MapRoute(
    "Default",
    "{id}/{controller}/{action}",
    new { controller = "Home", action = "Index", id = 0}
);

控制器:

public class HomeController : Controller
{
    public ActionResult Index(int id)
    {
        ViewBag.Message = "Welcome to ASP.NET MVC!";

        return View();
    }
}
相关问题