andrew Sullivan
andrew Sullivan

Reputation: 4084

routing in asp.net mvc

I have a route

routes.MapRoute(
  "BuildingProject",
  "BuildingProject/{action}/{id}",
  new {
    controller = "Home",
    action = "Index",
    id = ""
});

i want it to behave like default route ie for url that starts with BuildingProject like http://localhost:4030/BuildingProject/DeleteAll. I tried

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

It worked.But on typing localhost:4030/BuildingProject it is not redirecting to it's Index but showing error.
.How to do this.

Upvotes: 0

Views: 63

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038810

If your routes look like this:

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

    routes.MapRoute(
        "BuildingProject", 
        "BuildingProject/{action}/{id}", 
        new 
        { 
            controller = "Home",
            action = "Index",
            id = UrlParameter.Optional
        }
    );
}

then http://localhost:4030/BuildingProject/DeleteAll will call the DeleteAll action on Home controller and if you navigate to http://localhost:4030/BuildingProject, the Index action will be invoked on the same controller.

Upvotes: 1

Alexey Smolyakov
Alexey Smolyakov

Reputation: 840

Try this:

 routes.MapRoute("BuildingProject", "BuildingProject/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });

Upvotes: 0

Related Questions