71

I would like to enable a ASP.NET classic (ASMX) web service for HTTP POST and GET requests. I realise this can be done on a machine or application level by adding ...

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

.. to the machine.config or web.config. My question is can HTTP POST and GET requests be enabled per web service or web method level rather than per application or machine?

My web service is written in c# using net 3.5sp1.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Dean Bates
  • 1,927
  • 5
  • 25
  • 24

2 Answers2

53

Try to declare UseHttpGet over your method.

[ScriptMethod(UseHttpGet = true)]
public string HelloWorld()
{
    return "Hello World";
}
tanathos
  • 5,566
  • 4
  • 34
  • 46
46

Actually, I found a somewhat quirky way to do this. Add the protocol to your web.config, but inside a location element. Specify the webservice location as the path attribute, like so:

<location path="YourWebservice.asmx">
  <system.web>
    <webServices>
      <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
      </protocols>
    </webServices>
  </system.web>
</location>
BLΞND OnLine
  • 468
  • 5
  • 6
  • Only, you don't need the `location` part. `system.web` lives directly inside the main `` node. – vapcguy Mar 06 '18 at 22:29
  • 2
    @vapcguy That depends on what you're trying to accomplish. If you have multiple services but only one needs to support `GET`, this will allow it for just the one. If you don't care if it applies to all of them, then yes you could just put it in the general `system.web` section – ahwm Mar 05 '19 at 18:35
  • @ahwm If we want to use HttpPost method then we need to write it as `[ScriptMethod(UseHttpGet = false)]` Correct?? – Naman Upadhyay Mar 07 '19 at 05:05
  • @NamanUpadhyay Better to just use `[HttpGet]` and `[HttpPost]` tags in the code-behind. But I think you're missing his point - those tags have to do with if you are getting or posting data to anywhere. He's allowing for multiple locations from which to get/post data in the web.config. I only had one datapoint location so I found it unnecessary, but his comment/answer is accurate for his needs and perhaps others if they have multiple. Having multiple locations has nothing to do with what tag you use in your code-behind - that is dependent solely on if you are getting/sending (posting) data. – vapcguy Mar 07 '19 at 16:15