Search code examples
asp.netasp.net-mvcrazorroutesrouteconfig

How create RouteLink with custom routes registered in route config?


Im having this routes in RouteConfig:

routes.MapRoute(
                name: "AdminGroupsEdit",
                url: "admin/groups/{id}",
                defaults: new { controller = "Group", action = "Edit" }
            );
routes.MapRoute(
                name: "AdminGroupsEditStudent",
                url: "admin/groups/{id}/students/{studentid}",
                defaults: new { controller = "Student", action = "Edit" }
            );
routes.MapRoute(
                name: "AdminGroupsCreateStudent",
                url: "admin/groups/{groupId}/students/create-student",
                defaults: new { controller = "Student", action = "Create" }
            );

When im on page with iri:

admin/groups/1

there is a RouteLink:

@Html.RouteLink("Add", routeName: "AdminGroupsCreateStudent", routeValues: null, htmlAttributes: new { @class = "btn btn-success" })

But when i clicked on it asp.net going to route with name "AdminGroupsEdit". How can i create RouteLink to go to route with name "AdminGroupsCreateStudent"? If i need to pass groupId throw routeValues so how can i get it from Uri?


Solution

  • Couple of issues I can see with the way your currently routing so I've detailed below along with recommendations to resolve.

    1. In your example, all that the routing engine is able to understand is that your attempting to create a Url to the route name AdminGroupsCreateStudent. The routing engine is able to confirm it has a route with the name AdminGroupsCreateStudent, but then when trying to match the route values against the parameters, it's unable to continue as you've attempted to pass null and this route needs a parameter of groupId in order to succeed so it returns the route referring to the page your on.
    2. By creating a parameter mid-way into the Url, the routing engine expects a value for the route to render successfully. Sadly even attempting to assign the groupId parameter with the value UrlParameter.Optional, You would still end up with the same issue during runtime as once again the engine would is unable to do anything with a null parameter in the middle of Url as it cleverly knows it would break the link and is the reason why parameters are placed at the end or have default values.

    My recommendation's would be to do either of the following:

    Add groupId to the end of the routes Url and set the default value to UrlParameter.Optional.

     routes.MapRoute(
                    name: "AdminGroupsCreateStudent",
                    url: "admin/groups/students/create-student/{groupId}",
                    defaults: new { controller = "Student", action = "Create", groupId = UrlParameter.Optional }
                );
    

    Or if you need to have this Url in place, then just set a default value for groupId to handle the null and then manage the logic for this value in the Create action.

    routes.MapRoute(
                    name: "AdminGroupsCreateStudent",
                    url: "admin/groups/{groupId}/students/create-student",
                    defaults: new { controller = "Student", action = "Create", groupId = 0 } //0 == Unknown
                );