How to map Home/Action/id to just action/id?

后端 未结 3 1411
谎友^
谎友^ 2021-01-13 13:26

At the moment I have just this route defined

 routes.MapRoute(
                \"Default\",                                                
                \         


        
相关标签:
3条回答
  • 2021-01-13 13:39
     routes.MapRoute(
                    "NewRoute",                                                
                    "{id}",                           
                    new { controller = "Home", action = "Index", id = "" } 
                    );
    

    try that, as long as you default the controller and action it should work. Don't forget when generatig links with Html.Action to use the new route name.

    Apologies for formatting written on my iPhone

    0 讨论(0)
  • 2021-01-13 13:46

    look at this question How to Route /About to /Home/About

    It's not (exactly) the answer but I think it gives you the answer in reverse.

    0 讨论(0)
  • 2021-01-13 14:00

    You may have to be careful with a route like you're asking for because it may catch more than is intended.

    One workaround is to restrict that route by using constraints if possible.

    E.g. Check that id is numeric...

    routes.MapRoute("ActionRoute", 
             "{action}/{id}",
             new { controller = "Home", action = "Index", id="1" },
             new { id = @"\d{1,6}" }
    );
    

    That way you can still specify your generic default route at the end to catch the rest of the routes on the site if needed.

    0 讨论(0)
提交回复
热议问题