1

i all,

In a previous question, I asked how to define a custom route to handle the following URL:

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

I have a User object and a UserProfile object, but only a UserController that I want to be able to use for actions on both objects. I already have a method in UserController called Edit that handles edits on a User. But I also need a method for edits on a UserProfile. The answer to my routing question was the following route:

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

But given that custom route, where should I be declaring the edit action for a UserProfile, and what should it be called? It seems like I couldn't write another method in UserController called Edit because I already have one that handles User edits.

So I feel like I would end up with a need for two Edit actions to handle the following routes: "User/Edit" and "User/Profile/Edit". How do I get around this?

Thanks very much.

Community
  • 1
  • 1
Matt
  • 23,363
  • 39
  • 111
  • 152
  • 1
    Are you saying you need to distinguish between User/Edit and User/Profile/Edit ? – dotariel Nov 02 '10 at 17:49
  • Yes, exactly what I'm saying. Edited question to be more clear. – Matt Nov 02 '10 at 17:51
  • Why would you need to get around it? Isn't it quite representative of what you need? Or are they the same? – dotariel Nov 02 '10 at 17:52
  • Maybe that answers my question. Can I just have two action methods in UserController, both called Edit, but that take two different parameters? I need to be able to distinguish between the two actions because the actions are completely different depending on whether I'm editing a User or a UserProfile. – Matt Nov 02 '10 at 17:55
  • Personally i would use a different controller as i see it as 2 totally differenct action, since you also have 2 different object. You try to create an second edit action with a different parametertype indeed, it should be seen as different method by your application. Only not sure if it will work, haven't tried something like this before but seems an interesting issue to me – Rob Nov 02 '10 at 17:59
  • I think I understand what you are asking. If two routes point to the same action, then you need to overload your action. If the method signature is the same for both cases, the compiler will complain and you may have to create separate actions; ie - EditProfile() and EditUser() – dotariel Nov 02 '10 at 17:59
  • @saint, and naming both function Edit() but with different parameters. What should that result into? – Rob Nov 02 '10 at 18:02
  • @Rob - If you had Edit(User user) and Edit(Profile profile), I think the compiler would still not accept that, because of the way model binding works. However, if you have Edit(int id, string name) versus Edit(string name), then I believe you could use separate routes to get to the correct method. However, I agree that it is probably a better idea to go with a separate action and/or controller altogether. – dotariel Nov 02 '10 at 18:11
  • @Saint I get your example, but i don't see why the compiler wouldn't accept Edit(User user) and Edit(Profile profile). Doesn't it see the parameters just as different as in your example with Edit(int id, string name) versus Edit(string name)? Or am i still missing something? – Rob Nov 02 '10 at 18:15
  • So I was looking at this answer: http://stackoverflow.com/questions/894779/asp-net-mvc-routing-via-method-attributes, and even though they're using custom attributes to achieve routing, it definitely seems like it's possible to have some strange route that maps directly to a certain action, even if that action's name isn't in the URL. So could I call one action EditProfile and the other EditUser and still achieve the same routes I wanted before? ("User/Edit/userID" and "User/Profile/Edit/profileID" – Matt Nov 02 '10 at 18:22
  • 1
    FYI: I posted a new, clearer question about what we're discussing here: http://stackoverflow.com/questions/4080897/asp-net-mvc-routes-how-to-name-an-action-something-completely-different-than-in – Matt Nov 02 '10 at 18:27
  • @Matt, good one. Due to time restriction i can't continue the discussion now. But will check back tomorrowmorning. I'm very interrested in this issue! – Rob Nov 02 '10 at 18:30

1 Answers1

0

When the framework it's going to select what action to execute it first check the actions with the name required with a HttpPost ot HttpGet attribute that match the request, if not action is selected this way, then it select any action that match the name. So, if you have two actions with the same name with no HttpPost or HttpGet attributes, you can't control with action is going to get executed.

David Martinez
  • 423
  • 6
  • 13