Вопрос

Have the following link in Index.cshtml:

 @Html.ActionLink("Edit", "Edit", new { clientId = item.ClientId ,      overrideId=item.OverrideId})

and route as:

 routes.MapRoute(
          name: "Edit",
           url: "{controller}/{action}/{clientId}/{overrideId}",
          defaults: new { controller = "ClientOverride", action = "Edit", clientId =       UrlParameter.Optional, overrideId = UrlParameter.Optional }
       );

When redirect occurs from Index to Edit, the following URL is created:

.../clientoverride/edit/6?overrideId=1

while I would expect something like:

.../clientoverride/edit/6/1

Why so inconsistent URL is created? What can I do to make it look like a tree structure with clientoverride/edit/6/1 at the end?

Это было полезно?

Решение

Put this on top of all the rules used for clientoverride controller:-

    routes.MapRoute(
         name: "Edit",
          url: "{controller}/{action}/{clientId}/{overrideId}",
         defaults: new { controller = "ClientOverride", action = "Edit", clientId = UrlParameter.Optional, overrideId = UrlParameter.Optional }
      );

As your default route map is forming the url in this format/clientoverride/edit/6?overrideId=1. So, after changing the sequence it will solve your problem.

Другие советы

Try using Html.RouteLink instead of Html.ActionLink.

For links to routes that have more than one argument, you're better placed using @Html.RouteLink as it's for custom routes.

In your instance, you'd use @Html.RouteLink("Edit", "Edit", new { clientId = item.ClientId, overrideId = item.OverrideId})

The above example uses Html.RouteLink(LINKTEXT, ROUTENAME, VALUES)

The reason you're getting the URL you're seeing is that Html.ActionLink always assumes the default route format so any additional parameters end up getting turned into a query string.

Make sure your custom route is above the default route too.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top