0

I have an URL like this: http://website.com/Profile/Member/34

I need this URL runs like this: http://website.com/Profile/John

Given John as profile name for the user id=34.

Can anyone give me directions to do that?

Rubia Gardini
  • 815
  • 5
  • 16
  • 30
  • 1
    Are you trying to change the URL so that if somebody navigates to `http://website.com/Profile/Member/34` it will show as `http://website.com/Profile/John` in their browser? Or are you asking how to perform a lookup so the user can type `http://website.com/Profile/John` into the browser and it will find the member with user id = 34? – Tom Chantler Sep 29 '11 at 19:47
  • Yes Dommer, I want if someone navigates to Profile/Member/34 it shows Profile/John... is not a matter of controller route, I need a URL rewrite... – Rubia Gardini Oct 01 '11 at 05:50

2 Answers2

1

In global.asx you need to add a new route.

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapRoute(
            "Member", // Route name
            "Profile/{member}", // URL with member 
            new { controller = "YourController", action = "Profile"}
        );

    }

You will still need to implement the action that handles looking up the profile based on {member}.

Deekane
  • 143
  • 6
1

You have to add a custom route in the global.ascx.cs that will be used to redirect to the good controller. But I guess that "John" is not a unique value so you will have to keep the id in the Url, or if John is the username and is unique you can go with this url:

routes.MapRoute("Member", "Profile/{member}", new { controller = "Member", action = "Profile"});

Then in your controller you will have :

public ActionResult Profile(string username){
    //fetch from the db
}

If "John" is not a unique value I suggest you use :

routes.MapRoute("Member", "Profile/{id}/{member}", new { controller = "Member", action = "Profile"});

So your Url will look like http://website.com/Profile/John/34 and youre controller :

 public ActionResult Profile(int id){
        //fetch from the db
    }

Let me know if you need more help!

VinnyG
  • 6,883
  • 7
  • 58
  • 76
  • Hi VinnyG, actualy I need to keep my controller as Profile, my action as Member, and keep the search by user ID, but when it shows in URL, I need a rewrite for Controller/userName – Rubia Gardini Oct 01 '11 at 00:28