开发者

ASP.NET MVC, areas, routing and a controller factory

开发者 https://www.devze.com 2022-12-25 10:52 出处:网络
I\'ve an interesting problem with some routing with an ASP.NET MVC app. I\'m building a CMS and I\'ve got a catch-all handler that takes the URL and checks to see if there\'s some matching content in

I've an interesting problem with some routing with an ASP.NET MVC app. I'm building a CMS and I've got a catch-all handler that takes the URL and checks to see if there's some matching content in a database. If so, it displays it, otherwise we get a 404.

Now I've got all that working with some test data, I moved on to write a quick admin system. I thought I'd use some of the new Ar开发者_JAVA百科ea functionality baked into MVC 2, so I've created an area called Admin with a controller called Home. Now however I have a problem of the default HomeController in the Admin Area is being returned when requesting the application root path.

The problem is that there is no other HomeController for the 'root' application (the one hosting all of the areas), instead the root would be redirected to the my catch-all handler and populated from the database. So now the controller factory is returning the best matching controller, which it thinks is the admin area one, what I really need is for it to not match it at all - as it did previously.

Apart from renaming the Admin HomeController to something else, is there another solution?


In the Admin area, open the AdminAreaRegistration.cs

Add the correct namespace where your controllers are:

   public override void RegisterArea(AreaRegistrationContext context)
      {
         context.MapRoute(
             "Admin_default",
             "Admin/{controller}/{action}/{id}",
             new { action = "Index", id = UrlParameter.Optional }*,
             new string[] { "Stridh.Areas.Admin.Controllers" }*
         );
      }

Now do the same in the global.asax:

     routes.MapRoute(
         "Default", // Route name
         "{controller}/{action}/{id}", // URL with parameters
         new { controller = "Home", action = "Index", id = UrlParameter.Optional },
         new string[] { "Stridh.Controllers" } // Parameter defaults
     );


Edit... (sorry last answer was off topic).

Shot in the dark. Try changing the default maproute....

routes.MapRoute( "Default",
"{controller}/{action}/{id}",
new { controller = "Home", area="", action = "Index", id = "" }

or maybe area = null.

or changing the controller from home to something else.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号