views:

121

answers:

4

I have these 2 routes mapped out:

 routes.MapRoute(
                "Admin",
                "admin/{controller}/{action}/{id}",
                new { controller = "Admin", action = "index", id = "" }              
            );

and then I have:

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

            );

So the 2 routes are identical, except the first one has /admin prefixed in the URLS.

This is what is happening, I have no idea how to explain this:

When I go to:

www.example.com/user/verify

it redirects to

www.example.com/admin/user/complete

instead of

www.example.com/user/complete

The action Verify simply redirects to Complete like this:

return RedirectToAction("complete", "user");

And all the complete action does is populate the ViewModel, and then calls the view.

How can it be redirecting and adding the prefix /admin/ to the URL?

A: 

I believe it is redirecting to the Admin route because the Admin route is the first with all the matching parameters (controller and action in the case provided). If you want to use something like this you will need to either look into using areas (MVC2) or using a named route redirect.

NickLarsen
Indeed, ASP.NET MVC is lazy - regex speaking - when it comes to route matching.
Dan Atkinson
so your saying it IS actually appending the /admin to the URL by itself? that just doesn't make sense!
Blankman
The admin route doesn't have a 'complete' action, so not sure why it is even finding a match.
Blankman
It's matching because the parameters (controller, action and id) are all met. There is literally nothing distinguishing the first route from the second. It doesn't matter that /admin is at the beginning of the url. If this admin route is in another area, then you should use the area constraint, which has already been discussed in your other question - http://stackoverflow.com/questions/2156080/controller-ambigous-error-upgraded-to-mvc-2/2156419#2156419
Dan Atkinson
@Blankman you are essentially telling the routing API to look for the first route that handles having a controller and action specified by the call to `RedirectToAction(action, controller)`. The routes are checked in the order they are entered, so it will always land on your admin route first because it handles all the specified variables.
NickLarsen
@NickLarsen but I am telling it that I want a specific controller, so not sure why it finds a route in another controller? how can I be more specific w/o re-orderingmy route definitions (and not having to use RC 2)
Blankman
@Blankman You are not telling it that you want a specific controller actually, it is the routing API's job to find the controller you want based on the route. When you use the routing API to create a link, it is going to find the first match and use it, as it is doing. If you link directly to `user/complete` it will take you to the same controller. Routes don't belong to specific controllers, they belong to the route table which is used to find controllers.
NickLarsen
Thanks, but I just find that strange because I put RedirectToAction(action, CONTROLLER), since I am specifying the controller it should only look in that controller (one would think!!!) :)
Blankman
A: 

admin is your controller, you dont need an admin/controller/action the default route works just fine

all you need is an admin controller and the default route will find it for you

ie {controller}/{action}/{id}

will send /admin/addproduct to a controller named admin and an action called addproduct you only need to add routes if you want something custom for example

/products/televisions/hdtv/2

where products would be a controller and the last 3 are category,subcategory and pagenumber on the controller you point it to within your route.

hope that makes sense

minus4
I think that the OP is using areas with more than one controller with the same name.
Dan Atkinson
He could also be searching for the admin prefix to determine if he is an administrator or not also. I would see it as linking to the same controller, but the controller logic would determine if he should have access to the admin controls for that page or not.
NickLarsen
A: 

Not sure exactly how your controllers are structured, but you can add a constraint to the first MapRoute to limit it to the specific controllers you want the route to apply to:

routes.MapRoute( 
            "Admin", 
            "admin/{controller}/{action}/{id}", 
            new { controller = "Admin", action = "index", id = "" } ,
            new { controller = "[Some regex Expression - e.g. Admin]" }             
        ); 

Which will make the route only applicable for those controllers related routes. You can also use this tool to debug your routes. Depends how you have things structured, but like @NickLarson said - sounds like your using area functionality of MVC 2.

RM
A: 

mvc goes from top to bottom while matching router, that' why you are dealing with this problem

Jack