Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am making an application with MVC3 i have a URL like
where uname is the value of username of the user (for eg abc123)

http://localhost:5000/UserProfile/ShowProfile/abc123

i want to retrieve this value in controller

how can i retrieve it??
I have registered routing in global.asax.cs but have no idea how to retrieve it

my Action is

C#
public ActionResult ShowProfile(string customuserName)
       {         

       }
Posted

1 solution

Based on your example the Controller = UserProfile and Action=ShowProfile. Now coming to the point, check the routing for this kind of url

Your route should be:

C#
routes.MapRoute(
"Default",
"{controller}/{action}/{customuserName}",
new { controller = "UserProfile", action = "ShowProfile", customuserName = UrlParameter.Optional }
);
//Then the action will be:
public ActionResult ShowProfile(string customuserName)
       {         
 
       }

//so, change the parameter name that you defined in routing also.


I would like tell more about routing, but it will be distracting. Please read about the routing so that you can avoid routing mistakes.
 
Share this answer
 
Comments
Syed Salman Raza Zaidi 19-Jan-12 0:29am    
Yes the router has defined in the same way
Kethu Sasikanth 19-Jan-12 9:41am    
Do a quick test, like i did. Comment out all other routes except the above route. Make sure UserProfileController.cs is in Controllers folder. You have not talked about the View(cshmtl) so I assume it is in proper place. Put a break point in ShowProfile method, hit-f5 and try!
If you still see a problem, let me know what exact problem is - page error, control is not going to the method or customuserName is null.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900