Question

I have to route my mvc application like , www.MySite.com/User123. Just like facebook do, http://facebook.com/UserId will describe all the user information. I tried this But Its not working.

routes.MapRoute( _
          name:="Default", _
          url:="{id}", _
          defaults:=New With {.id = "user"} _
      )
Was it helpful?

Solution

If you're trying to route to a specific controller and action without having them in the URL, you need to specify them as defaults. Your route for this would be something like this:

routes.MapRoute(
    name: "Profile",
    url: "{id}"
    defaults: new { controller = "Profile", action = "Show" }
);

This would give you a route that would map "http://www.mysite.com/user123" to the Show action on the ProfileController, passing user123 as the ID parameter.

public class ProfileController : Controller
{
    public ActionResult Show(string id)
    {
        ...
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top