开发者

Why does my route get redirected, this doesn't make any sense to me

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?


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.


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


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.


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜