Page 1 of 1

Change the default directory MVC searches for Controllers

#1 coden4fun  Icon User is offline

  • if(coden4fun.NotTired){ ContinueToCode(); }
  • member icon

Reputation: 27
  • View blog
  • Posts: 696
  • Joined: 27-February 09

Posted 06 March 2010 - 03:30 PM

I've been working with ASP.net MVC professionally now for an entire month, and I have learned so much about it; however, I didn't like the default directory that MVC searches for your web pages. In the Global.asax.cs file you have the following code in the RegisterRoute (which as you can tell from the name registers routes in MVC).

 public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

        }



As you can see the default directory is Home, and if anyone has created a none empty ASP.Net MVC project then you should be quite familiar with the HomeController.

I wasn't sure why they chose Home to be the default directory when I found it much more logical to make it Public.

So do the following

 public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Public", action = "Index", id = "" }  // Parameter defaults
            );

        }



Changing the Home to Public makes the route search in the Public folder for the content when the website is fired up. Ah ha! I thought this is what I want, however, I wasn't done.

I had to do the following 2 things to get the website up and running without getting the Yellow Screen of Death.

First, I had to change the controller name from HomeController to PublicController. Remember it's not a controller if the prefix isn't Controller with a capital C.

Second, in the Views directory I had to rename Home to Public.

There as simple as that. I have changed the default directory of my website in ASP.Net MVC 1.0 and 2.0 to Home to Public.

Home this serves a purpose ;)

Is This A Good Question/Topic? 0
  • +

Page 1 of 1