3

I would like to be able to have custom pages that are not tied to a controller. These custom pages will all be routed to a default controller. Then there are other pages that are not custom that have Controller classes set up as normal. I can't get the routing rules to work for both situations.

I have routing rules that look like this:

    routes.MapRoute( _
        name:="Default", _
        url:="{controller}/{action}", _
        defaults:=New With {.controller = "Home", .action = "Index"} _
    )

    routes.MapRoute( _
         name:="Custom Page", _
         url:="{name}", _
         defaults:=New With {.controller = "Page", .action = "Index"} _
     )

This situation works for the static controllers, but not the custom pages. It always tries to find the controller with the custom name (url:="{name}") which doesn't exist, so I get a "Resource not found" error.

If I reverse the rules, then the custom pages work correctly, but the static pages do not.

I know that the problem is that both rules overlap each other, so whichever one is first is always used. Ideally I would like to be able to tell the routing to look for a controller named X, if it exists use that. If it doesn't exist, redirect to Y controller and pass in X as a parameter. Right now if the controller does not exist, it doesn't continue to the next routing rule, it just says the resource cannot be found.

Is something like this possible?

tereško
  • 58,060
  • 25
  • 98
  • 150
user1333935
  • 61
  • 1
  • 6

2 Answers2

0

I have not tested this, but I think you may have to use {*name}, basically a catch all route as seen in this answer => .Net MVC Routing Catchall not working.

You would also need to switch your routes around so that the catch all is most specific, like so:

routes.MapRoute( _
     name:="Custom Page", _
     url:="{*name}", _
     defaults:=New With {.controller = "Page", .action = "Index"} _
 )

routes.MapRoute( _
    name:="Default", _
    url:="{controller}/{action}", _
    defaults:=New With {.controller = "Home", .action = "Index"} _
)

You also may need to use {*catchall} instead of {*name}, but I am not certain on that.

Community
  • 1
  • 1
ryanulit
  • 4,983
  • 6
  • 42
  • 66
0

Here I Provide a solution for the problem. For each Route, you can define an alternative route, the alternative route will be used in case there is no controller for the route.

   var defaultRoute = routes.MapReplaceableRoute(
              name: "Default",
              url: "{controller}/{action}/{id}",
              defaults: new { controller = "Home", action = "Index", id = 
  UrlParameter.Optional    }
          );

        var route = routes.MapRoute(
            name: "Dynamic",
            url: "{name}",
            defaults: new { controller = "Dynamic", action = "Index" });
        
        defaultRoute.AlternativeRoute = route;

The clue is in the latest line!

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Siyamand
  • 54
  • 2
  • Thank you! This solved my issue. The code in the blog post is slightly incomplete, so anyone else looking to use this should read the post (to understand it) and grab the library that the author links to in the end of the post. – sorenhk Aug 22 '14 at 09:11