ASP.NET MVC Routes: How to define custom route?

In your Global. Asax file in the RegisterRoutes() method do the following.

Up vote 0 down vote favorite share g+ share fb share tw.

I've looked online for an answer to this question, but I honestly can't seem to find a good reference for MVC routes. I have a UserController for my User objects. One can Edit, Save, View, etc. on the User, so I have actions in that controller to handle each of those.

That's all pretty straightforward. But I've recently created a new UserProfile object that one can also edit, view, etc. Rather than create an entirely new controller just for the UserProfile, I'd like to make use of the existing UserController. So to view a user's profile, I'd like the URL to be: example.com/User/Profile/{userProfileID} And to edit, I'd like the URL to be: example.com/User/Profile/Edit/{userProfi... Each of these actions in the UserController will return a different view page.

How would I go about defining routes to handle this structure? Thanks very much. Asp.net-mvc routes custom-routes link|improve this question edited Oct 1 '11 at 4:24drneel32412 asked Nov 2 '10 at 17:10Mega Matt1,27021747 66% accept rate.

In your Global. Asax file in the RegisterRoutes() method do the following: routes. MapRoute( "ProfileRoute", "User/Profile/{action}/{userProfileID}", new { controller = "User", action = "Index" }); As pointed out by the comments...this must come BEFORE the Default route.

2 Also, as an FYI since it caught me, it has to be declared before the Default route – Calgary Coder Nov 2 '10 at 17:17 Very good point...edited my answer. – Dismissile Nov 2 '10 at 17:26 So this route looks like it will handle the case of /User/Profile/Edit/{userProfileID}, because I can have an action in the UserController of Edit, but what about just viewing the profile? Won't I need to write an action method called View or something to be able to do that?

How would I just have /User/Profile/{userProfileID}? Also, to be able to handle the Edit case, what would the action in the UserController need to be named, given the custom route? It looks like "Index", but how would I be able to call it 'Edit'?

– Mega Matt Nov 2 '10 at 17:27 The last line in that route declaration are the defaults. Whatever is supplied in the {action} part of your URL will override that default. – Mike Sickler Nov 2 '10 at 17:43 So if I already have an Edit action defined in UserController to handle edits on a User, then this won't work because I'll need another action called Edit for the UserProfile: "User/Edit" and "User/Profile/Edit" -- both actions would be in the same controller.

– Mega Matt Nov 2 '10 at 17:49.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions