Search code examples
c#asp.net-mvc-5routeconfig

How to configure routeConfig.cs to take input directly after root domain


Check the code bellow. Here in route i want to give user enter input of username in url just like example.com/username but problem with that RouteConfig.cs is this cant take input like that. This will only take controller/method format. Please advice me how can i achieve domain/username type input form user? I want to serve that request from Test method bellow

Currently RouteConfig.cs file:

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

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

Test method:

public ActionResult Test(String username)
        {
            return View();
        }

Solution

  • Try this.

    routes.MapRoute("Id"
                  , "{id}"
                    , new { controller = "Home", action = "Test"});