Question

ASP.Net MVC3 is cool and all but I have this question more out of curiosity than programming issues. Say I have an area called 'Cosmos' within which there is a 'StarController' with an 'Index' action.

Now, if I had to create an action link to this, what is the difference between these two statements?

Html.ActionLink("Stars", "Index", "Stars", new { area = "Cosmos" }, null)

and

Html.ActionLink("Stars", "Index", "Cosmos/Stars")

By the looks of it, they the exactly the same thing. If it indeed does the same thing, what is the use of the anonymous type { area = "Cosmos" } ?

Était-ce utile?

La solution

In the second example you set the controllerName argument to Cosmos/Stars which is wrong. A controller cannot be called that way. It generates the correct result because the helper simply uses the argument as is, but you have assumed that your routes will be of a certain style. At the moment you change your routing definition this link will continue to generate the same markup which could be wrong. In the first example you no longer rely on any hardcoded url pattern. It will always generate the correct url no matter how your routes and areas are configured.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top