0

This is a continuation of a couple of previous questions I've had. I have a controller called UserController that I'd like to handle actions on two types of objects: User and UserProfile. Among other actions, I'd like to define an Edit action for both of these objects, and within UserController. They'll need to be separate actions, and I don't mind calling them EditUser and EditProfile in the controller, but I'd prefer if the URL's looked like this:

http://www.example.com/User/Edit/{userID}

and

http://www.example.com/User/Profile/Edit/{userProfileID}

Does anyone know how to achieve these routes, given the restraint for the actions being in the same controller?

And for context, previous questions are here and here

Thanks.

Community
  • 1
  • 1
Matt
  • 23,363
  • 39
  • 111
  • 152
  • @Matt maybe should also mention the previous question where we were discussing earlier. Gives people some more information – Rob Nov 02 '10 at 18:29
  • @Matt, you already found a good solution to this? – Rob Nov 03 '10 at 10:04

3 Answers3

6

Just an suggestion, but can't you do something like this to map the correct routes?

routes.MapRoute(
    "ProfileRoute", // Route name
    "User/Edit/{userProfileID}", // URL with parameters
    new { controller = "User", action = "EditUser" } // Parameter defaults
);

routes.MapRoute(
    "ProfileEditRouet", // Route name
    "User/Profile/Edit/{userProfileID}", // URL with parameters
    new { controller = "User", action = "Editprofile" } // Parameter defaults
);

EDIT: Then in your controller create two seperate methods called EditUser(guid userId) and Editprofile(guid userId)

Rob
  • 6,731
  • 12
  • 52
  • 90
4

You could try something like the following: (untested)

routes.MapRoute(
    "EditUser",
    "User/Edit/{userID}", 
    new { controller = "User", action = "EditUser" });

routes.MapRoute(
    "EditProfile",
    "User/Profile/Edit/{userProfileID}",
    new { controller = "User", action = "EditProfile" });

EDIT:

Using MvcContrib (available from http://mvccontrib.codeplex.com/) the syntax is slightly clearer:

(using MvcContrib.Routing;)

MvcRoute
    .MappUrl("User/Edit/{userID}")
    .WithDefaults(new { controller = "User", action = "EditUser" })
    .AddWithName("EditUser", routes);

MvcRoute
    .MappUrl("User/Profile/Edit/{userProfileID}")
    .WithDefaults(new { controller = "User", action = "EditProfile" })
    .AddWithName("EditProfile", routes);
Patrick McDonald
  • 64,141
  • 14
  • 108
  • 120
1
using MvcContrib.Routing;

public class UserController : Controller
{
    [UrlRoute(Path = "User/Edit/{userID}")]
    public ActionResult UserEdit(int userID)
    { 

    }
}
dotjoe
  • 26,242
  • 5
  • 63
  • 77