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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With