Reputation: 24789
In my web app. i want to setup a route like this:
/document/quality/index
/document/general/index
/document/quality/detail/[id]
/document/general/detail/[id]
As you can see i have two kind of documents: general and quality. What is the best way to set this up in my global.asax file? I tried the following, but i don't get it work:
routes.MapRoute(
"QualityDocument",
"Document/Quality/{action}/{id}",
new { controller = "Document", action="Index", id= ""}
);
routes.MapRoute(
"GeneralDocument",
"Document/General/{action}/{id}",
new { controller = "Document", action = "Index", id = "" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
EDIT
I have it working right now, but when i change the action="" in my asax file it doesn't work anymore:
Global.asax:
routes.MapRoute(
"GeneralDocument",
"Document/General/{action}/{id}",
new { controller = "Document", action = "blaat", id = "" }
);
DocumentController:
public void blaat(int? id)
{
Response.Write("algemeen");
// return View();
}
Now i get the Resource not found error. But when I use index instead of blaat it is working. How come?
Upvotes: 0
Views: 721
Reputation: 2605
you should be able to just prefix the route with "Document" and then set the defaults like:
routes.MapRoute("DocumentView",
"Document/{controller}/{action}/{id}",
new { controller = "General", action="Index", id = "" }
);
remember with routing you want to be completely defining! when setting out the route. dont let those unexpected routes through :D
Check out Scott Hanselman presentation at MIX very funny and your pick up some great tips!
http://www.hanselman.com/blog/Mix09FirstHalfRollupAndSessionVideos.aspx
Upvotes: 1
Reputation: 532465
Perhaps add in the controller attribute, but constrain it to be the document controller.
routes.MapRoute(
"QualityDocument",
"{controller}/Quality/{action}/{id}",
new { controller = "Document", action="Index", id= ""},
new { controller = "Document" }
);
routes.MapRoute(
"GeneralDocument",
"{controller}/General/{action}/{id}",
new { controller = "Document", action = "Index", id = "" },
new { controller = "Document" } );
Upvotes: 1
Reputation: 8448
Try this:
routes.MapRoute(
"QualityDocument",
"Document/Quality/index",
new { controller = "Document", action="Index" }
);
routes.MapRoute(
"Default", // Route name
"Document/Quality/details/{id}", // URL with parameters
new { controller = "Document", action = "Details", id = "" } // Parameter defaults
);
Upvotes: 0