6

It seems that ASMX implicitly does not allow the OPTIONS verb. I'm posting this question because I'm using jQuery AJAX calls with POST which first queries the server for available OPTIONS before the POST verb** is issued.

By default Web.config maps all verbs to legacy ASMX, as shown in this partial configuration sample, so everything should be routed properly:

<system.webServer>
    <requestFiltering>
        <verbs>
          <add verb="OPTIONS" allowed="true"/>
          <add verb="GET" allowed="true"/>
          <add verb="POST" allowed="true"/>
        </verbs>
    </requestFiltering>

<handlers>
    <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode"
       type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

However the HTTP Response is always 405 for an OPTIONS request. For example, the following request is given:

OPTIONS http://localhost:35920/MarkupTransfomer.asmx HTTP/1.1
Host: localhost:35920
Access-Control-Request-Method: POST

and always results in:

HTTP/1.1 405 Method Not Allowed
Server: Microsoft-IIS/7.5
X-AspNet-Version: 2.0.50727

The jQuery AJAX call looks like the following, a recommendation from a fairly recent Encosia blog post for working with ASMX:

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "http://localhost:35920/MarkupTransfomer.asmx",
        data: "{'hi'}",
        dataType: "json"
    });

** Note: I don't want to switch the client to use GET instead of POST.

Summary question:
Can I configure ASP.NET to allow ASMX to handle OPTIONS Requests inherently without error?

Other ideas I've considered

  • I can look for a way to tell jQuery.ajax(..) not issue the OPTIONS verb before POST?

  • If what I'm asking from legacy ASMX isn't possible, I've considered two other potential ideas:

    1. implement a System.Web.IHttpHandler, stick it in the web.config handlers section to manage only the verbs=OPTIONS requests to path=*.asmx outside of the default ASMX behaviour. This would be a doable workaround.
    2. I can switch over to using WCF. However I want to know if ASMX is too unwieldly first.

Before I take action, I want to query the community for things I might have overlooked, or for better options (no pun).

UPDATE #1

Yep, my web service is being hosted on a different domain. Apparently there are a plethora of issues to navigate with Same Original Policy. This update is definitely worthwhile information to the context of the question all things considered!
However I want to ensure this question stays at the ASMX server and HTTP levels. Although I might be facing related browser issues in the near future, those aren't of consequence to solving the HTTP protocol level of this question. Thanks.

John K
  • 28,441
  • 31
  • 139
  • 229
  • Apparently the OPTIONS header being issued is necessitated by Same Origin Policy not unlike that described in the title of this question http://stackoverflow.com/questions/1099787/jquery-ajax-post-sending-options-as-request-method-in-firefox So I'm putting my idea of getting rid of the OPTIONS call on the back burner for now. – John K Nov 21 '11 at 05:25
  • Did you manage to get it work with `HTTP OPTIONS`? I am trying to access .asmx with `HTTP HEAD` to know asmx is dead or alive and get same error. – Murali Murugesan Aug 23 '16 at 16:05
  • @murali I did not get OPTIONS to work in this scenario – John K Aug 23 '16 at 16:12

2 Answers2

1

Looks like maybe it doesn't apply because I don't see it in the question, but my answer to this was that In the web config I had to remove the instruction to <remove name="OPTIONSVerbHandler" />

Kudos to the person who posted it near the end of this SO question

-1

I don't know that ASMX was ever able to handle OPTIONS, even for AJAX requests. What exactly do you want to return in the case of an OPTIONS verb? What does the client intend by using OPTIONS?

Also, I've looked into this from the point of view of ASP.NET dealing with the OPTIONS verb. In my case, it was prohibited at the level of the root web.config, not at the ASP.NET or ASMX level. The default mapping for that verb is

<add path="*" verb="*" type="System.Web.HttpMethodNotAllowedHandler" validate="True" />

This corresponds to the 405 error you received.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • I'm dealing with OPTIONS because of Same Origin Policy not unlike that described in this question http://stackoverflow.com/questions/1099787/jquery-ajax-post-sending-options-as-request-method-in-firefox What I'm hoping to do is satisfy its OPTIONS request with Access-Control* headers and whatever else I need. My root config doesn't have that mapping however you tipped me off that I might consider the inherited configuration which I haven't done yet. – John K Nov 21 '11 at 04:51
  • 1
    I did try making a custom handler (from my ideas section at bottom of question) `` and that rids the 405 message and returns a response. – John K Nov 21 '11 at 04:52
  • Indeed, through this process I realized I'm accustomed to dealing with web service calls from the same domain. My foray into mitigating Same Origin Policy brings realization I've been sheltered from some interesting scenarios. – John K Nov 21 '11 at 04:58
  • 1
    If you need to do anything interesting, then I suggest you stop working with ASMX at once. It has very little flexibility, and is no longer being enhanced, nor are bugs being fixed. – John Saunders Nov 21 '11 at 05:07
  • Very sage advice. This isn't the first time I've run into it. Fortunately this time around I have the option to choose the backing web service technology. I thought ASMX would be a quick way to get something running. However I think Same Origin Policy is as much to blame (a rightful culprit) no matter my choice. I'd almost like to crack this ASMX nut just for the sake of it. – John K Nov 21 '11 at 05:22
  • 1
    You might be able to, just keep in mind that ASMX has almost no extensibility. If you need to do anything out of the ordinary, you'll be out of luck. – John Saunders Nov 21 '11 at 05:23