ASP.NET MVC Html.ActionLink URL - Coding Method

I create a number of actions in MVC controllers.

public ActionResult DoSmth1(string token) public ActionResult DoAnother2(string token) 

And when I need to call ActionLink ..

 =Html.ActionLink<SomeController>( x=> x.DoSmth(item.property), item.property) =Html.ActionLink<AnotherController>( x=> x.DoAnother(item.property), item.property) 

... it generates me different urls:

 /Some/DoSmth/stringvalue /Another/DoAnother?property=stringvalue 

Where to set the way to create the url? I have no ideas ... ((


Well, a got some light: - if the property names are the same as those used in the routing scheme - for example, the controller, action and identifier - MVC will always use the route builder (/ c / a / id).

This helps a bit (so - name the parameter "id" as much as possible ))

But the general problem remains valid ...


should be named just like the token in the route

Exactly - I had my first idea.

But now I only have the default route ({controller} / {action} / {id}), but still have a URL with a "property" in outline ... This is pretty weird.

  • there is also a fraudster - to create an exact route that matches the given controller with its parameter names - it seems that this will be the final answer - but I still do not want to do this ((
+6
asp.net-mvc html-helper routing actionlink
source share
1 answer

You do not show your routes, but in this example you will almost certainly click on different routes. The argument of your action should be called the same as the token in the route, so that the generated URL matches the route marker using the ActionLink lambda form. Anything that does not match the routing token will be added as a query string parameter, just like your second URL. Viewing the query string parameter is convincing evidence that the name you passed implicitly (the β€œproperty” in this case) does not match the route marker. Since you get different results with the same marker name, I thus end you, you click different routes. By the way, I recommend building links with RouteLink instead of ActionLink, so you can be sure which route you will follow.

+4
source share

All Articles