6

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'
Derek Flenniken
  • 477
  • 3
  • 11

2 Answers2

6

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.

Community
  • 1
  • 1
Arnis Lapsa
  • 45,880
  • 29
  • 115
  • 195
  • Didn't think to statically route them, way to hung up on finding the pattern! Still seems there should be a more dynamic way, so I don't have to add a new route for each page. – Derek Flenniken Jun 29 '09 at 07:30
  • I thought the same, but I'm not so optimistic anymore. Anyway - routing through attributes looks like a silver bullet to me. :) – Arnis Lapsa Jun 29 '09 at 08:02
  • Almost like there is an implicit trailing slash so in the end "/Info" and "/Admin/" end up hitting the same route because "/Info" gets treated as "/Info/". And vice-versa; if I re-order the routes, the first route still catches both. In that case it's like it's treating "/Admin/" like "/Admin". I'm checking out the attribute based routing. Definitely looks promising. – Derek Flenniken Jun 29 '09 at 23:13
  • Yeah, but might get quite complicated. I'm afraid that I'll have to dive deep into this soon too. Post here as answer your progress if you manage to find anything interesting. Going to mark this as favorite. :) – Arnis Lapsa Jun 30 '09 at 01:16
  • In the first example this would provide a working route for `/Info`, but it can still be called on `/Home/Info`. He can we purge that route or redirect it to only use `/Info`? – QFDev Apr 16 '15 at 09:35
0

You can use Route Attributes.

In your route config file you should have.

        routes.MapMvcAttributeRoutes();
        AreaRegistration.RegisterAllAreas();
        //code below should already be in your route config by default
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

Then above every action you can have a route attribute.

 [Route("info")]

You can even get more advanced with these attributes by adding parameters, and/or subfolders

 [Route("blog/posts/{postId}")]

You can put the above attribute on any action, and it will appear as if it's originating from the blog controller. However, you don't even need a blog controller. Also the {} signify the parameter, so just make sure your action is taking the same parameter as what's in the curly braces. In this case the parameter would be

string postId
guyfromfargo
  • 823
  • 8
  • 9