Sukhjeevan
Sukhjeevan

Reputation: 3156

ASP.NET URL Routing help needed in asp.net 4.0 application

I am going to implement routing feature in my asp.net4.0 application and following the given link:

URL-Routing

This is fine if an application has limited no. of pages but my application has lots of pages. so i have to write code [ routes.MapPageRoute("","",""); ] many times. Can we map all pages by looping through any collection classes or by any other method.

Thanks

Upvotes: 1

Views: 161

Answers (1)

Thanasis Ioannidis
Thanasis Ioannidis

Reputation: 3231

If you have a standard pattern for your url and your file structure you can use the placeholders in the mapped url too

eg. If you can map every "{controller}/{action}/{id}" to "/Pages/{controler}/{action}.aspx"

For example, i build a site that has this folder structure

Project Folder Structure

Under the root folder there is the Views folder where all my pages reside. Under the Views folder there is one subfolder for every "controller" (there is no controllers in webforms, but I follow the MVC conventions here) Under the controllers subfolders there are the aspx pages that represent different actions

The aspx page names are the same for each controller ("Index.aspx","Add.aspx","Edit.aspx" etc)

So I can have a general mapping rule

routes.MapPageRoute("GeneralAction", "{controler}/{action}/{id}", "~/Views/{controler}/{action}.aspx");

I don't need different rules for different pages as long as the folder structure follows this pattern

Now

"/Patient/Add" will map to "~/Views/Patient/Add.aspx"
"/Incident/Add" will map to "~/Views/Incident/Add.aspx"
"/Patient/Edit/31" will map to "~/Views/Patient/Edit.aspx" (with id=31)

etc, all matching this one rule above.

of course if you want to override this rule you can define more specific routes BEFORE defining this one.

Upvotes: 1

Related Questions