1

I'm trying create a ASMX webservice that can perform a HTTP GET request. I have the following simple snippet of code to illustrate what I've already done.

using System.Web.Script.Services;
...

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string HelloWorld(HttpContext context)
{
 return context.Request.Params.Get("userId").ToString();
}

In addition to this, I've also added the following nodes in my Web.config file

<webServices>
  <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
  </protocols>
</webServices>

The problem that I'm facing is that I'm constantly getting the dreaded "System.Web.HttpContext cannot be serialized because it does not have a parameterless constructor" error message whenever I try to debug this webservice. I have no idea what the problem is, and I would really appreciate any assistance that is offered to get me out of this quandary. I realize that HTTP GET requests are supposed to be very simple, but I'm really uncertain of what the cause of my frustrations are.

1 Answer 1

4

I think you want

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string HelloWorld(int userId)
{
    return userId.ToString();
}

You can specify parameters in the function signature and you can access the HttpContext as Context (a property on the base class WebService) if you need it.

Sign up to request clarification or add additional context in comments.

1 Comment

In my personal experience the [ScriptMethod(UseHttpGet = true)] does not effect the service method at all. It does not force the method to only use GET and without it you can still call the method with GET as long as you've allowed it in your web.config

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.