Introducing PageMethods

(Une introduction en français est disponible grâce à Améthyste)

Linking to a web page is very easy, both in simple HTML and in ASP.NET. Linking to a page that really exists, passing the right parameters, and parsing these parameters, is a bit different.

The problem

Let's take a simple example. You want to call a page that displays information about a customer. The page expects a customer ID. Let's say that ID is an integer.

Here is how a URL to call such a page would look like: http://myserver/Customer.aspx?CustID=12

If you're not the developer who created the page, how do you know the name of the parameter? How do you know the type of this parameter?
Either you have to look deep in the code of the page you want to invoke (do you always have that code at hand, by the way? Not so sure), or you look in the documentation related to that page (does such documentation exist? Is this documentation up-to-date? Did Joe update the documentation when he quickly renamed the parameter from CustID to CustomerID?).

This gives you an idea about the limitations coming with the default way of linking or redirecting to a page. But let me give you some more insight about other limitations:

  • you refer to a page by its file name (No check at compile time, so if you make a mistake, you'll know only when someone tries to access the page. This means that you're never sure what you deliver is 100% safe. When you realize you've made a mistake, it's late and you have to redeploy the application after fixing the problem)
  • you never know for sure what parameters or combinations of parameters each page expects
  • you have to know the exact name of each parameter
  • you pass parameters by concatenation of strings (this is work, code is not easy to read, and not nice)
  • you have to parse and convert parameters by yourself (in ASP.NET, parameters passed on URLs are available through Request.QueryString, only as strings)
  • a page must validate the parameters it receives (Check parameters are not null. Is the type of each parameter correct? Are the parameters within the range of valid values?)

The solution

PageMethods takes care of your URLs. It proposes a solution to define structured URLs for each of your pages, as well as a clean and simple way to call them.

The idea is based on strict page inputs and declarative parameter binding. With PageMethods, each page exposes a set of methods that represent the different ways to call the page.
All you have to do to start benefiting from sharp URLs is to add methods to your pages, and mark these methods with attributes provided by PageMethods.

Here is how the code for a Customer page would look like:

Solution comparison

A bit of code tells a lot, so let's compare the source code for the PageMethods solution and for the standard solution.

Here is how the code for a page expecting two parameters can look like with the standard solution:

Here is how the code for the same page looks like with PageMethods:

Now, here is how we would get a link to this page with the standard solution:

... and with PageMethods:

That already tells a lot...

You can quickly get a deeper overview on how PageMethods is used by reading the tutorial.
You can also decide to learn more about the features and benefits.

Part of the metaSapiens network: metaSapiens | .NET and Java Jobs | Fabrice's weblog | SharpToolbox | JavaToolbox | Tuneo | LINQ in Action | MapShares | Clair de Bulle
Managed Windows Shared Hosting by OrcsWeb