0

I have written REST service in C#. Here is the sample.

[ScriptService]
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class TradeRestService : System.Web.Services.WebService
{

    [WebMethod]
    public string HelloWithRequest(String request)
    {

        return "You sent "+request;
    }
    [WebMethod]
    public string HelloWithoutRequest()
    {

        return "Hello without request";
    }

When I call the REST service from Advanced Rest Client, I can only call HelloRequest() method successfully. I cannot pass parameters to the methods which accept them.

When I call HelloRequest() method with parameter, I get the error.

System.InvalidOperationException: Missing parameter: request.
at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)
at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request)
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

Can anyone tell me how to pass parameter? Which option to use?

Dixit Gokhale
  • 601
  • 1
  • 12
  • 32
  • Wouldn't you have to parse the request parameter from the URL, if it's a GET? – pkr May 07 '14 at 14:01
  • I am adding URL parameter key and value. But still I get the error. – Dixit Gokhale May 07 '14 at 14:04
  • 1
    Something like http://localhost:1234/TradeRestService.asmx/HelloWithRequest?request=something ? – pkr May 07 '14 at 14:12
  • Yes like that only.. If I am sending XML payload as a string, how should i do it? What should be "Content-Type"? I tried "application/xml", "text/html" but with no luck:( – Dixit Gokhale May 07 '14 at 14:19
  • Avoid sending XML payload through URL parameters and try to do it by POST if you have to. – gerodim May 07 '14 at 14:26

1 Answers1

0

First make sure that the rest client you are using is trying to send the date by GET and not by POST. Then check that GET is enabled. Try adding the following over your web method declaration:

[ScriptMethod(UseHttpGet = true)]

Referenced Post

Of course you can enable POST in the same way and send your request using POST...

Community
  • 1
  • 1
gerodim
  • 131
  • 8
  • My method HelloWithRequest(String request) is not accepting xml if I try to send xml as a string. In Advanced Rest Client in Raw tab, I paste the xml and press send button. I get error: System.InvalidOperationException: Request format is invalid: application/xml charset=UTF-8.. BTW I used all possible request types. – Dixit Gokhale May 08 '14 at 07:16
  • If you DO need to send xml I recommend that you do it through the POST http method. You can check an example of the POST request if you navigate to the asmx page with a web browser. Also don't forget to enable POST/GET methods for your web service either as I mentioned above or at the web config.(check the link I posted) – gerodim May 08 '14 at 07:26
  • I tried but with no luck. Can you show me an example? – Dixit Gokhale May 08 '14 at 09:19