Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routing in ASP.NET MVC, showing username in URL

Tags:

I'm trying to make a route so I can show the username in the URL like this:

http://localhost1234/john

Here Is my routeconfig:

 routes.MapRoute(
                name: "users", // Route name
                url: "{username}", // URL with parameters
                defaults: new { controller = "Home", action = "Index", username = "" } // Parameter defaults
            );

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

Here is my HomeController:

 public ActionResult Index(string username = "Test")
 {
   return View();
  }

First of all, the URL Is not changed. When I set username = "Test" inside my route-config, the URL is not changed.

Second, I can't navigate to my other controllers. If I change the URL to http://localhost123/Welcome, nothing happens. It should redirect me to a new page.

What am I doing wrong here?

If I change the order of the routes, I can navigate to other pages, but the username Is not displayed In the URL.

I have googled and all of the answers on this subject says that I should use a route like the one above.