tags:

views:

85

answers:

4

Hello

I have a view that both edits and creates "usergroups". And I have a "usergroup-detailsview" (Admin/Usergroup/43) where I have 2 links:

Html.RouteLink("Edit", "UsergroupEdit", 
    new { usergroupID = Model.Usergroup.UsergroupID })

Html.RouteLink("Create", "UsergroupCreate")

In my global.asax I got:

routes.MapRoute("UsergroupEdit", "Admin/Usergroup/Edit/{usergroupID}",
    new { controller = "UsergroupsAdmin", action = "Edit" } );

routes.MapRoute("UsergroupCreate", "Admin/Usergroup/Edit", 
    new { controller = "UsergroupsAdmin", action = "Edit" } );

The first one where the int is passed in renders:

Admin/Usergroup/Edit/87

But the second one renders like:

Admin?Length=24

How can I fix this route?

/M

+2  A: 

To remain sane ;) and to keep things clear use two routes:

routes.MapRoute(
    "UsergroupEdit",
    "Admin/Usergroup/Edit/{usergroupID}",
    new
    {
        controller = "UsergroupsAdmin", 
        action = "Edit"
    });

routes.MapRoute(
    "UsergroupCreate",
    "Admin/Usergroup/Create",
    new
    {
        controller = "UsergroupsAdmin",
        action = "Create"
    });

Also from code I see you have a controller name UsergroupsAdmin..If you have somewhere there also a Usergroups controller, you better get rid of UsergroupsAdmin and just decorate "admin" actions in Usergroups controller with [Authorize action filter.

For Authorize you can implement your own Role provider so you can check your requirements there. It will keep things very clean and maintainable.

If you decide to keep Edit route, just make sure to have only one like this:

routes.MapRoute(
    "UsergroupEdit",
    "Admin/Usergroup/Edit/{usergroupID}",
    new
    {  
        controller = "UsergroupsAdmin",  
        action = Edit",
        usergroupID = 0
    });

and check in controller action where usergroupID=0, render create view.

mare
+1  A: 

What you have should work just fine. Perhaps you have a typo somewhere?

I just tried this out by putting those two calls into my Index.aspx view within the HomeController and I got the following output:

<a href="/Admin/Usergroup/Edit/123">Edit</a> 
<a href="/Admin/Usergroup/Edit">Create</a>

Here was my RegisterRoutes method.

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


    routes.MapRoute("UsergroupEdit", "Admin/Usergroup/Edit/{usergroupID}",
        new { controller = "UsergroupsAdmin", action = "Edit" });

    routes.MapRoute("UsergroupCreate", "Admin/Usergroup/Edit",
        new { controller = "UsergroupsAdmin", action = "Edit" });

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

}

Most likely, you're running into a problem with another route defined before those routes.

Haacked
why are you getting down voted? Phil Haack is a Product Manager working on ASP.NET MVC. Seriously, he might know something...
Redbeard 0x0A
When I change order ir renders..... /Admin/Edit and then /Admin/Edit?usergroupID=78... I guess that will have to do for now. Or is there some other way of fixing this?
molgan
@molgan, Like Phil Haack said, the problem most likely occurs because of another route defined before thouse routes.
çağdaş
A: 

What does your controller method "Edit" look like (the method signature)?

Are you using the int? datatype (a nullable int)?

I usually get the whole ?Length=24 thing when I am trying to map to a action and have messed up the parameters somehow.

You should also consider trying @Haacked's recommendation of reordering the routes in your globals code. I don't know who is down voting the guy, but he knows a lot about ASP.NET MVC.

Redbeard 0x0A
A: 

Changed order, although didnt fix it completely, just made it work

molgan