0

I have some project management system. If I want to open project with id=123 I redirect to controller action using address:

http://myhost:67845/Projects/id=123

How I can use user-friendly link for the same action like:

http://myhost:67845/Projects/John-can-develop-asp-net-site-for-you
Matt
  • 22,721
  • 17
  • 71
  • 112
ZedZip
  • 5,794
  • 15
  • 66
  • 119

2 Answers2

1

You could update the default route:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{title}",
        new { 
            controller = "Home", 
            action = "Index", 
            title = UrlParameter.Optional 
        }
    );
}

and then have a controller action which will take the title as argument

public ActionResult Index(string title)
{
    ...
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

By using the default routing, you can use http://myhost:67845/Projects/123 without any change, since 123 will be treated as id.

However, if you want that kind of friendly name, you have to make a change in your controller.

In the second case, in your Projects controller (for default routing) this will be handled by the method called Index. This method should have a property string id.

Then inside your method, you need a way to convert between John-can-develop-asp-net-site-for-you and 123. This can either be that John-can-develop-asp-net-site-for-you is the name of project 123, or you will have to store the friendly name in the database as well to make the conversion.

Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
  • Ok, and As I see need to have a generator of this kind of friendly names which must be unique to have a 1=1 relationship with id – ZedZip Dec 19 '11 at 09:27
  • @Oleg - That is correct. If two id's have the same friendly name, the address might be ambiguous, and you wouldn't like that. Even if the name is friendly for the user, it has to point to a specific entry just like an id has to point to a specific entry. – Øyvind Bråthen Dec 19 '11 at 09:44