Click here to Skip to main content
15,891,951 members
Articles / Web Development / ASP.NET
Tip/Trick

USE OF MapPageRoute Method IN ASP.NET(WEBFORM) ROUTING

Rate me:
Please Sign up or sign in to vote.
4.30/5 (6 votes)
18 Dec 2013CPOL3 min read 33.4K   1  
Routing Technique in ASP.NET (WEBFORM) AND USE OF MapPageRoute

Introduction

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.

Background

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:

http://abc.com/employee/2/shiv

And this is also requesting on server by default:

http://abc.com/maincss/css/style.css

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.

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

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 http://abc.com/manager/2/shiv this pattern type of url to route. On any physical path, i.e., http://abc.com/employee.aspx in global.asmx file on application start, call the method:

C#
void Application_Start(object sender, EventArgs e)
  {
    RegisterRoutes(RouteTable.Routes);
  }

  void RegisterRoutes(RouteCollection routes)
  {
       routes.MapPageRoute("Employee",
       "{designation}/{id}/{name}", "~/employee.aspx");
      routes.MapPageRoute("Employee",
      "{designation}/{id}/{name}", "~/employee.aspx", false);
      routes.MapPageRoute("Employee",
      "{designation}/{id}/{name}", "~/employee.aspx", true,
      new RouteValueDictionary { { "designation", "Employee" },
      { "id", "emp1001" }, { "name", "ABC" } });
      routes.MapPageRoute("Employee",
      "{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}$" } });
       routes.MapPageRoute("Employee",
       "{designation}/{id}/{name}",
       "~/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" }   });
  }

Properties:

  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.
    HTML
    <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.

License

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



Comments and Discussions

 
-- There are no messages in this forum --