starcorn
starcorn

Reputation: 8531

how to get a parameter from dynamic url in asp.net MVC4?

I have the following url

http://localhost/user/MyUserName

How do I get "MyUserName" from that url? So I can use it at the server side to retrieve the information I want to present to the client side?

In Django it was very simply to archive that. But how do I do that in ASP.net MVC4?

EDIT

routes.MapRoute(
      name: "Default",
      url: "{controller}/{action}/{id}",
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
      );

Upvotes: 1

Views: 1753

Answers (1)

Wahid Bitar
Wahid Bitar

Reputation: 14084

I don't know what did you name your route parameter

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}", 
     new {controller = "Home", action = "Index", id = UrlParameter.Optional}, 
     null,
     new[] {"Project.Web.Controllers"});

But I suppose it's Id anyway try this:

var value = RouteData.Values["Id"];

Update :

In your case you should define a special route before " on top of " the default one :

routes.MapRoute(
    "ForUser",
    "User/{id}", 
     new {controller = "User", action = "UserInfo", id = UrlParameter.Optional}, 
     null,
     new[] {"Project.Web.Controllers"});

Upvotes: 3

Related Questions