Mr John
Mr John

Reputation: 119

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();
        }

Upvotes: 0

Views: 114

Answers (1)

Vivek Nuna
Vivek Nuna

Reputation: 1

Try this.

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

Upvotes: 0

Related Questions