Click here to Skip to main content
15,868,292 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Is there any way to catch a bad parameter passed into a web service via URL? for example, given the following trivial webservice method:

public class MyWebService : System.Web.WebServices
{
    [WebMethod(Desciption="My WebService")]
    public int MyMethod( int number )
    {
       return number * 2;
    }
}


Call it via a URL, but pass in an invalid int:

http://localhost/MyWebService.asmx/MyMethod?number=three

This will display a rather ugly error message to the user. Is there any way to catch this error and handle it more elegantly?

Thanks.
Posted
Updated 12-May-10 9:45am
v2

1 solution

I guess you need to accept a string as a param, which is counterintuitive to what you want, in order for it to be accepted, and for your code to parse it and decide if it's valid or not, and how to respond.
 
Share this answer
 
Comments
Miszou 12-May-10 18:10pm    
Well, the function actually already accepts a string as a workaround to this problem, but I was trying to tidy it up and make it more intuitive to the caller.

You're right that I can use a string, call In32.TryParse on the value and then deal with it as necessary. But it would seem to be "better" to use an Int in the first place and let the framework handle the erroneous input. I just want it to handle it in a slightly friendlier fashion!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900