Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 routing issue with multiple parameter

I am defining my action link like this:

 @Html.ActionLink("Visit profile By Name", "UserProfile", "User", new { UserId = 2, UserName = "Sandeep" }, null)

Which generates the link:

http://localhost:2010/User/UserProfile/Sandeep/2

If username == string.empty (as username is coming dynamically), then the link becomes

http://localhost:2010/User/UserProfile?UserId=2

But in that case I want my link like to look like this:

http://localhost:2010/User/UserProfile/2

Route table value:

       routes.MapRoute(
           "UserName", // Route name
           "User/UserProfile/{UserName}/{UserId}", // URL with parameters
           new { controller = "User", action = "UserProfile", UserName = UrlParameter.Optional, UserId = UrlParameter.Optional } // Parameter defaults
       );
like image 593
sandeep Avatar asked Dec 12 '25 15:12

sandeep


1 Answers

change your route

routes.MapRoute( "UserName", // Route name 
              "User/UserProfile/{UserName}/{UserId}", // URL with parameters 
               new { 
                   controller = "User", 
                    action = "UserProfile", 
                    UserName = "UserProfile",  /*change this */
                     UserId = UrlParameter.Optional 
                  }  // Parameter defaults 
                 );

change UserName = UrlParameter.Optional to UserName = "UserProfile"

like image 70
Praveen Prasad Avatar answered Dec 14 '25 12:12

Praveen Prasad