views:

48

answers:

2

Anyone know if it's possible to move the HomeController and Home related views into the Areas directory?

I'm trying to keep my root directory nice and clean and avoid having the ~/Views and ~/Controllers directories if I can. Furthermore, I can see it causing some confusion by having to explain that those root folders are for homepage stuff only and everything else lives in the Areas folder. It just doesn't fit with my sense of organization, I guess.

The closest I've come is using the following for my Home area route registration:

context.MapRoute(
    "Home_default",
    "Home/{action}/{id}",
    new { controller="Home", action = "index", id = UrlParameter.Optional }
);

... But this doesn't catch just plain "www.mydomain.com/". For that I need to tell my "catch all" route in the Global.asax to somehow send that request over to my Home area. Simply adding area="Home" to the route data didn't work. A request for "/" is still looking for the HomeController and Views in my root directory.

Any ideas?

A: 

It's possible that IIS is adding "default.aspx" or some default document name to the request URL before applying your routes, in which case your example won't work. (This can be configured in IIS.)

You would need a route like this

context.MapRoute(
   "Home_root"
   ,"/"
   ,new { controller="Home", action="index", id=UrlParameter.Optional }
);

but, of course, routes can't start with "/", "~", etc.

or

context.MapRoute(
   "Home_root"
   ,"default.aspx{*parameters}" /// or .htm, .asp, .html or whatever IIS may be adding 
   ,new { controller="Home", action="index", id=UrlParameter.Optional }
);

HTH. In a hurry so I haven't tried to compile any of this.

David Lively
Thanks, but I couldn't get that working. I'm testing locally with VS dev environment. I checked it out with Phil Haack's route debugger and still can't get the catch-all route pointing to my Home area.
Derek Hunziker
A: 
// These additions allow me to route default requests for "~/" to the "home" area
engine.ViewLocationFormats = new string[] { 
    "~/Views/{1}/{0}.aspx",
    "~/Views/{1}/{0}.ascx",
    "~/Areas/{1}/Views/{1}/{0}.aspx", // new
    "~/Areas/{1}/Views/{1}/{0}.ascx", // new
    "~/Areas/{1}/Views/{0}.aspx", // new
    "~/Areas/{1}/Views/{0}.ascx", // new
    "~/Views/{1}/{0}.ascx"
};
Derek Hunziker