asp.net mvc routing id parameter

前端 未结 6 912
自闭症患者
自闭症患者 2020-12-29 07:04

I am working on a website in asp.net mvc. I have a route

routes.MapRoute(
    \"Default\", // Route name
    \"{controller}/{action}/{id}\", // URL with pa         


        
6条回答
  •  情深已故
    2020-12-29 08:03

    @Parminder The default route can handle all action with one parameter "id". And I think not every action need this parameter. So I change my default route

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

    and you can add a new route:

    routes.MapRoute("errorpage", "yourcontroller/errorpage/{errorno}",
        new {controller="controllername", action="errorpage"});
    

    this just handle your controll name is "controllername". If you want to handle all controller, you can add this:

    routes.MapRoute("errorpage", "{controller}/errorpage/{errorno}",
        new {controller="controllername", action="errorpage"});
    

    This method will create very much code in global.asax if you need a lot of custom route.

提交回复
热议问题