How Html.Action() Work

By | March 23, 2012

Let’s take the example:

@Html.Action("Latest", "Episode")

What this will do is to invoke the “Latest” action method in the “Episode” controller. But what really happens behind the scenes is NOT a direct invoke; it will actually start from the beginning of the ASP.NET MVC execution pipeline using “Latest” and “Episode” as Route values for the keys “action” and “controller” respectively.

This means that you should pay very good attention to your Routes definition in the Application_Start() in Global.asax; Html.Action() will try to match the best route in your defined routes according to the RouteValueDictionary created above (action and controller) along with any additional route values provided in the overload.

So bottom line don’t assume that Html.Action will invoke action directly, and make sure that your Route unit tests always cover your back when you need to change your Route definitions, or your risk your Html.Action() methods to be ruined.

Leave a Reply

Your email address will not be published. Required fields are marked *