Create ASP.NET MVC3 ActionLink with scope

ASP.Net MVC3 is cool and that's it, but I have this question more out of curiosity than programming problems. Let's say I have an area called "Space" that has a "StarController" with an "Index" action.

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

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

      

and

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

      

In appearance, it is the same. If this is indeed the same, what is the use of the anonymous type {area = "Cosmos"}?

+3


source to share


1 answer


In the second example, you are setting the argument controllerName

to Cosmos/Stars

, which is incorrect. The controller cannot be named that way. It generates the correct result because the helper is just using the argument as is, but you assumed that your routes would be styled in a certain way. At the moment you change the definition of routing, this link will continue to generate the same markup, which may not be correct. In the first example, you no longer rely on any hardcoded URL. It always generates the correct url no matter how your routes and areas are configured.



+3


source







All Articles