2

we have one project in asp.net mvc 5 when i run my project and click on some button for go to the next page then we are getting the url like wwww.abx.com/project/projectname .

where project is my controller name and projectname is Action name

so if i want so url like

wwww.abx.com/projectname

then this is possible ?

please if any one have some solution so please give it

here is my route.config file code

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

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

Thanks

ashish
  • 25
  • 7

2 Answers2

0

you have to create a new route:

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

routes.MapRoute(
name: "ProjectName1",
url: "Projectname1",
defaults: new { controller = "Project1", action = "Projectname1" }
);

routes.MapRoute(
name: "ProjectName2",
url: "Projectname2",
defaults: new { controller = "Project2", action = "Projectname2" }
);

routes.MapRoute(
name: "ProjectName3",
url: "Projectname3",
defaults: new { controller = "Project3", action = "Projectname3" }
);



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


}
Hamed Javaheri
  • 538
  • 3
  • 13
  • we want for more then 5 pages. – ashish Aug 05 '17 at 12:37
  • wwww.abx.com/project/projectname1 wwww.abx.com/project/projectname2 wwww.abx.com/project/projectname3 wwww.abx.com/project/projectname4 wwww.abx.com/project/projectname5 and i want like wwww.abx.com/projectname1 wwww.abx.com/projectname2 wwww.abx.com/projectname3 wwww.abx.com/projectname4 wwww.abx.com/projectname5 – ashish Aug 05 '17 at 12:43
  • Check it please. – Hamed Javaheri Aug 05 '17 at 13:20
0

If you would like to do this for only one controller - add a custom route as suggested by @Hamed Javaheri, or use attribute mapping as described here

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

            routes.MapRoute(
                name: "Project",
                url: "projectname",
                defaults: new { controller = "project", action = "projectname", id = UrlParameter.Optional }

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

If you want this to work for many / most routes then you might want to change the default mapping, somethin similar to this question:

ASP.NET MVC Default URL View and

ASP.NET MVC Routing with Default Controller

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

            routes.MapRoute(
                name: "Default",
                url: "{action}",
                defaults: new { controller = "project", id = "" }
            );
        }
ironstone13
  • 3,325
  • 18
  • 24