Categories
Uncategorized

ASP.NET routing for www.website.com/users/yasser

For the urls like www.website.com/user/username, the following route can be used.

[sourcecode language=”csharp”]
public class RouteConfig
{
 public static void RegisterRoutes(RouteCollection routes)
 {
 routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);

routes.MapRoute(
 “UserProfile”,
 “Users/{username}”,
 new { controller = “User”, action = “Index”, username = string.Empty }
 );

routes.MapRoute(
 “Default”,
 “{controller}/{action}/{id}”,
 new { controller = “Home”, action = “Index”, id = UrlParameter.Optional }
 );
 }
}
[/sourcecode]

[sourcecode language=”csharp”]
public class UserController : Controller
{
 public ActionResult Index(string username)
 {
 return View();
 }
}
[/sourcecode]

Further Reading :
User routing in ASP.NET MVC for urls like www.website.com/users/jeffAtwood