I have a new MVC3 project setup, and I added a folder inside my controller named “area1”.
I then put my controller inside that folder named abc.cs:
public class abc : Controller
{
//
// GET: /abc/
public ActionResult Index()
{
return Content("abc index");
}
}
public class MyAreaRegistration : AreaRegistration
{
public override string AreaName
{
get { return "My Area"; }
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"abc",
"area1/{controller}/{action}/{id}",
new { controller = "abc", action = "index", id = UrlParameter.Optional }
);
// And so on ...
}
}
Now when I go to:
/area1/abc/index
I get an error page.
The resource cannot be found.
I put a breakpoint in the RegisterArea method and it doesn’t get called.
What am I doing wrong?
Other Few questions:
-
can I put my areas folder and controller code anywhere in the project, or does it have to be inside the main controllers folder?
-
can I have my views in my areas folder also?
I’m not sure what you mean by
Since your controller is a class, it doesn’t make sense to add a folder to it.
The process to create an area goes something like this:
In Visual Studio, right click on the project root, and select
Add|AreaEnter the name for the area –
Area1or what have you.VS will create
~/areas/area1, and add the Views, Controllers and Models folders.Add, for instance,
IndexController.csto the Areas1/Controllers folder.Add a view, “Index.cshtml”, to the Area1/Views folder
Rebuild the solution.
Open http://localhosdt/area1/index in your browser, and you should see your view.