Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Routing Root Level Views

I thought this would be fairly easy, but I'm totally baffled.

I want one controller's views to be at the root level of the application, rather than in a subdirectory for that controller, but I cannot figure it out.

I'd like to have these two urls:

/Info - This should action "Info" on controller "Home"

/Admin/ - This should be action "Index" (default) on controller "Admin"

So far no matter what I've tried, the first route will end up catching both. I can't seem to separate the two.

That Info page doesn't even need a controller, it' static, but I do want to use a master page. There may be a much easier way to pull this off, but I haven't figured that out either.

All I can think of that would work, would be to create an Info controller, and move Views/Home/Info to Views/Info/Index, but that has a certain smell to it.

I was able to do this in rails using:

  map.connect ':controller/:action/:id'
  map.connect ':action', :controller => 'home'
like image 665
Derek Flenniken Avatar asked Jun 29 '09 06:06

Derek Flenniken


1 Answers

You just need proper routes. In your case:

routes.MapRoute(
                "Info",
                "Info",
                new { controller = "Home", action = "Info" }

routes.MapRoute(
                "Admin",
                "Admin",
                new { controller = "Admin", action = "Index" }

But i recommend you this approach.

If you need to change default physical location of views/partialviews,
check out how to create custom view engines.

like image 91
Arnis Lapsa Avatar answered Oct 09 '22 07:10

Arnis Lapsa