Monday, February 28, 2011

How To Use Html.Action With Areas

Microsoft created Areas to make our lives easier. The sad thing is that some little details can make your life worse. We are going to take a look at one of those things - Html.Action. When you are using Areas in your application, Html.Action can generate errors. The typical error you will see is "The controller for path '/whateverpath/' was not found or does not implement IController".

That is real helpful isn't it? You can tell a lot from that error right? NOT!!!! That error might lead you to believe that some controller is having an issue. That is not the case! Instead, that error means that you have not set up your Html.Action item correctly. To get your areas to cooperate with Html.Action, you have to set your link up like this.:

Html.Action("YourMethod", "YourController", new { area = "YourArea" })

You might be wondering what you should do if you are using Html.Action and you are using a controller that is NOT in an area. Well let me tell you so you can stop wondering. You set up the action method like this:

Html.Action("YourMethod", "YourController", new { area = "" })

And that is it!!

Smooches,

Kila Morton

Problem:

"The controller for path was not found or does not implement IController"

Solution:

Use
Html.Action("YourMethod", "YourController", new { area = "YourArea" })
if you are referencing something inside of an area.

Use
Html.Action("YourMethod", "YourController", new { area = "" })
if you are not referencing something outside of the areas