Click here to Skip to main content
14,332,309 members


Rate this:
4.30 (6 votes)
Please Sign up or sign in to vote.
4.30 (6 votes)
18 Dec 2013CPOL
Routing Technique in ASP.NET (WEBFORM) AND USE OF MapPageRoute


When we try to apply routing in ASP.NET webform, then it might happen that many requests with the same pattern URL occur which match with route path pattern defined in global.asmx and it is responsible for run time error.


I found it during a project, that my js file and CSS file linked on master page having the same pattern of URL, which I used to route on a physical path, and on load event of webform I used to find the value of parameters passed in route path that’s why it goes in run time exception.

This is what I was expecting:

And this is also requesting on server by default:

Because it is a link of CSS file on master page.

It was not only the file, there were several files of the same kind (6 files of js,css).

So first, I used page load event of my employee.aspx page but it was not the exact solution because on master page 6 similar pattern of URL is present which calls 6 times employee.aspx and checks whether it is the right request or not.

Using the Code

So first, I used page load event of my employee.aspx page but it was not the exact solution because on master page 6 similar patterns of URL are present which call 6 times employee.aspx and check whether it is the right request or not.

protected void Page_Load(object sender, EventArgs e)
       if (!IsPostBack)
           string id = Page.RouteData.Values["Id"].ToString();
           if (id != "css" && id != "js")

Then I did some R&D on RouteCollection.MapPageRoute method, it has 5 overloads. These are:

  1. MapPageRoute(String<routeName>, String<routeUrl>, String<physicalFile>)
  2. MapPageRoute(String<routeName>, String<routeUrl>, String<physicalFile>),Boolean<checkPhysicalUrlAccess>)
  3. MapPageRoute(String<routeName>, String<routeUrl>, String<physicalFile>, Boolean<checkPhysicalUrlAccess>, RouteValueDictionary<defaults>)
  4. MapPageRoute(String<routeName>, String<routeUrl>, String<physicalFile>, Boolean<checkPhysicalUrlAccess>, RouteValueDictionary<defaults>, RouteValueDictionary<constraints> )
  5. MapPageRoute(String<routeName>, String<routeUrl>, String<physicalFile>), Boolean<checkPhysicalUrlAccess>, RouteValueDictionary<defaults>), RouteValueDictionary<constraints>, RouteValueDictionary<dataTokens>)

Let's look at an example to use all these overloads of MapPageRoute() method: suppose we have this pattern type of url to route. On any physical path, i.e., in global.asmx file on application start, call the method:

void Application_Start(object sender, EventArgs e)

  void RegisterRoutes(RouteCollection routes)
       "{designation}/{id}/{name}", "~/employee.aspx");
      "{designation}/{id}/{name}", "~/employee.aspx", false);
      "{designation}/{id}/{name}", "~/employee.aspx", true,
      new RouteValueDictionary { { "designation", "Employee" },
      { "id", "emp1001" }, { "name", "ABC" } });
      "{designation}/{id}/{name}", "~/employee.aspx", true,
      new RouteValueDictionary { { "designation", "Employee" },
      { "id", "1001" }, { "name", "ABC" } },
      new RouteValueDictionary { { "designation", @"^[a-zA-Z\s]{1,40}$" },
      { "id", @"\d{1,4}" },
      { "name", @"^[a-zA-Z\s]{1,40}$" } });
       "~/employee.aspx", true, new RouteValueDictionary {
       { "designation", "Employee" },
       { "id", "emp1001" },
       { "name", "ABC" } },
       new RouteValueDictionary { { "designation",
       @"^[a-zA-Z\s]{1,40}$" },
       { "id", @"\d{1,4}" },
       { "name", @"^[a-zA-Z\s]{1,40}$" } },
       new RouteValueDictionary { { "accounttype", "123" },
       { "acnumber", "5678" },
       { "Accountholder", "Ranvijay" }   });


  1. RouteName- Define the name of route in case of multiple route, You can leave it blank, ""
  2. RouteUrl-Define the pattern and parameters (string)
  3. PhysicalFile- Contain the name of Physical File name (string)
  4. checkPhysicalUrlAccess- Checks whether the request can access (boolean)
  5. defaults- Default value in case of request by route name(RouteValueDictionary)
  6. constraints- Defines the constraint on parameters(RouteValueDictionary)
  7. dataTokens- Associated data which further you can use in logics.

Points of Interest

You can access the DataTokens via the RouteData property and use them in your custom logic.

Description of each overload is as follows:

  1. First overload contains name route path/pattern/parameter and physical path.
  2. Second contains whether the request can access of the physical file true/false
  3. Third one for defining default value and request can access the page by call route name only in this case, i.e.
    <a href="Employee"> Shiv</a>
  4. It contains the constraint which you can apply on each parameter.
  5. It contains datatoken which is nothing, just an associated value which you can use in your logics.

That’s all I learned on this topic. Corrections or suggestion are most welcome.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Comments and Discussions

-- There are no messages in this forum --
Posted 18 Dec 2013


1 bookmarked