Search code examples
asp.net-mvccustom-routes

ASP.NET MVC Routes: How to name an action something completely different than in URL


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.


Solution

  • 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)