Dealing with missing query string parameters

65 views
Skip to first unread message

Thomas Koch

unread,
Jan 12, 2009, 11:02:20 AM1/12/09
to PageMethods
Hi - does anyone have a suggestion for dealing with missing query
string parameters?

If I have a method in a code behind file like this:

[PageMethod(true)]
private void ShowProjectInfo(int projectId) {
...
}

and the client is URL hacking in the browser, he might issue
"ShowProjectInfo.aspx" with any parameters, or with illegal values.
Currently PageMethods will generate an ArgumentException which is
fine, but I really would like to specify some default value for
missing parameters.

Currently it seems to me that the only way to deal with missing/
illegal parameters is to write a try-catch clause around the
"PageMethodsEngine.InvokeMethod(...)"

Regards
Thomas

Fabrice Marguerie

unread,
Jan 12, 2009, 1:35:05 PM1/12/09
to PageMethods
In the current version, a try-catch block is the only way to catch
missing/illegal parameters.

If you want to specify default values for missing parameters, what you
can to is use several page methods.
For example, you can have :

void ShowProjectInfo(int projectId)
{
ShowProjectInfoWithDetails(projectId, false);
}

void ShowProjectInfoWithDetails(int projectId, bool includeDetails)
{
// Do the real thing
}

Note: method overloading is not supported by PageMethods, as indicated
in the FAQ (http://metasapiens.com/PageMethods/faq).
If you want to hide the method names in the URL (the PageMethod
parameter), you should use URL rewriting (see the FAQ).

Of course this can get annoying if you have a lot of optional
parameters.

Another option is to use reference types (such as strings) for your
parameters. As long as the ParamRequired attribute is not applied to
such a parameter, a missing value for this parameter won't be signaled
by an ArgumentException.
Of course, you then have to cast the parameters to their real type
(e.g. from string to int or bool).

As a conclusion: there is no specific support for what you're trying
to achieve, but you should be able to do it with what is provided.
Of course, you can also suggest an improvement to the source code to
be integrated in a later release :-)

Fabrice

Thomas Koch

unread,
Jan 12, 2009, 3:03:02 PM1/12/09
to PageMethods
Hi Fabrice - thank you for your reply.

I'll give your suggestions a look.

I'd love to participate, but do not have the time. I guess you hear
that all the time. :-)

Regards
Thomas



On 12 Jan., 19:35, Fabrice Marguerie <fabrice.margue...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages