ASMX ScriptService mistake: Installation and configuration
AJAX, ASMX Mistakes and Misconceptions, ASP.NET By Dave Ward. Updated August 9, 2012Note: This post is part of a long-running series of posts covering the union of jQuery and ASP.NET: jQuery for the ASP.NET Developer.
Topics in this series range all the way from using jQuery to enhance UpdatePanels to using jQuery up to completely manage rendering and interaction in the browser with ASP.NET only acting as a backend API. If the post you're viewing now is something that interests you, be sure to check out the rest of the posts in this series.
Continuing my series of posts about ASMX services and JSON, in this post I’m going to cover two common mistakes that plague the process of getting a project’s first ASMX ScriptService working: Installing System.Web.Extensions into the GAC and configuring your web.config.
System.Web.Extensions (aka ASP.NET AJAX)
The ability for ASMX services to return raw JSON is made possible by two key features originally added by the ASP.NET AJAX Extensions v1.0:
- JavaScriptSerializer – The JavaScriptSerializer class is the actual workhorse that translates back and forth between JSON strings and .NET CLR objects. Though less powerful than WCF’s DataContractJsonSerializer and third-party libraries like Json.NET, JavaScriptSerializer is likely all you’ll ever need for simple AJAX callbacks.
- ScriptHandlerFactory – There are several more classes behind the scenes*, but the ScriptHandlerFactory is the tip of the iceberg that you’ll need to remember during configuration. Redirecting ASMX requests through this HttpHandler is what coordinates the pairing of ScriptService with JavaScriptSerializer to provide automatic JSON handling.
Though both of these classes appear in the System.Web.Script namespace, they actually reside in ASP.NET AJAX’s System.Web.Extensions assembly. That has different implications depending on which version of ASP.NET your site targets:
- 1.x – No support for ScriptServices. A custom HttpHandler coupled with a third party library like Json.NET is your best bet (if anyone has a good tutorial on doing this under 1.x, let me know so that I can link to it).
- 2.0 – ScriptServices are available in ASP.NET 2.0 with the installation of the ASP.NET AJAX Extensions v1.0.
- That means that the ASP.NET AJAX installer needs to be run on the server that hosts your site, not just on your local development machine.
- For some of a ScriptService’s features to work in medium trust (i.e. shared hosting), the System.Web.Extensions assembly needs to be in your server’s global assembly cache (GAC). Don’t waste your time trying to make it work in your site’s /bin directory; insist that the extensions be properly installed on the server.
- 3.5+ – As of .NET 3.5, System.Web.Extensions ships with the framework. No additional assemblies need be installed.
* If you’re interested in the internals, I highly recommend downloading the ASP.NET AJAX Extensions v1.0 source and taking a look at ScriptHandlerFactory, RestHandlerFactory, and RestHandler. Though the classes have changed slightly since v1.0, they are still very similar.
Rerouting the ASMX handler via web.config
With the System.Web.Extensions assembly installed in the GAC, the remaining configuration step is an element in your site’s web.config. To take advantage of the ScriptService functionality, ASP.NET must be instructed to reroute ASMX requests through the ScriptHandlerFactory instead of ASP.NET’s standard ASMX handler.
This step is often unnecessary. The project templates in ASP.NET 3.5+ include all the necessary configuration elements, and ASP.NET 2.0 sites created with the “AJAX Enabled” templates are also pre-configured correctly.
However, if you find yourself unable to coax JSON out of an ASMX ScriptService, verifying your web.config is one of the best first steps in troubleshooting the issue. Whether due to a web.config generated by an older project template, accidental modification, or other issues, missing the httpHandlers web.config setting is a very common pitfall.
What should appear varies slightly depending on which version of ASP.NET your project targets. Regardless of your framework version, the config elements should be added to the <httpHandlers> section and are the only elements necessary. The variety of other config items required for the UpdatePanel and ScriptManager aren’t crucial to the ScriptService functionality.
ASP.NET 2.0 (with the ASP.NET AJAX Extensions installed)
<configuration> <system.web> <httphandlers> <remove path="*.asmx" verb="*" /> <add path="*.asmx" verb="*" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> </httphandlers> </system.web> </configuration>
ASP.NET 3.5
<configuration> <system.web> <httphandlers> <remove path="*.asmx" verb="*" /> <add path="*.asmx" verb="*" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> </httphandlers> </system.web> </configuration>
ASP.NET 4
Thankfully, ASP.NET 4 has taken steps to reverse the trend of ever-enlarging baseline web.config files. By moving common configuration items such as the ScriptService’s HttpHandler to the default machine.config, each individual site need not include those configuration elements in their specific web.config files.
Unless you go out of your way to manually remove their HttpHandler, ASMX ScriptServices will work automatically in any ASP.NET 4 site.
Similar posts
What do you think?
I appreciate all of your comments, but please try to stay on topic. If you have a question unrelated to this post, I recommend posting on the ASP.NET forums or Stack Overflow instead.
If you're replying to another comment, use the threading feature by clicking "Reply to this comment" before submitting your own.
3 Mentions Elsewhere
- uberVU - social comments
- Using jQuery with ASP.NET 2.0 | Cérebro eletrônico
- Sending JSON to an ASMX Web Service « Personal Portal



Our clients also encounter the configurable issue for routing *.asmx extension here http://www.sunblognuke.com/Support/Forums/forumid/2/threadid/238/scope/posts.aspx. Thanks for sharing the tips.
Thanks Dave. I have been trying to access my old .asmx web services from an iPhone application using json for 3 days now. After following your instructions for editing the web.config, everything started working. I am very grateful to you for posting this.
Hi, Just wanted to say thanks for the great resource! You’re tips and observations have been priceless.
I’m trying to get a webservice running in Mono (.NET 3.5) to return Json, so far without any luck, but I have noticed what I think may be a typo in your example xml snippet.
In your httpHandler addition, the Culture=neutral attribute is part of the add tag, whereas if you look at the xml given over here :
http://vampirebasic.blogspot.com/2009/04/aspnet-ajax-in-mono.html
…the Culture is -inside the type attribute- of the add tag. Mono will throw an error if the web.config is modified as shown on this page (at least for the 3.5 section, have not tried the other). Note also that neutral is NOT in quotes above, unlike an xml attribute value, which leads me to believe it somehow just got copied into the wrong place when you posted it above.
You’re right. I’m not sure when/how that happened, but the
Culture=neutralis supposed to be part of thetypeattribute. I corrected the snippets in the post. Thanks for pointing that out.hi & thanks for your post … I Use My Service as ASMX . Locally it works But when i Upload it on the server , it does not work and show 401.1 UnAuthorized error . I try it with SVC service but i get same result !!!
& when i config Allow user to all (in web .config) , i get Exception about machin.config ?!!!!
please help me…
thanks in Advanced
If you’re getting a 401 error, that probably means you’re trying to access a service that’s secured either by IIS or ASP.NET before you’ve authenticated. If it needs to be available to unauthenticated users, you should move the service to a location that isn’t configured to be secure.
hi and thanks for your help BUT h found the solution … (a little wonder)
when i set
<customErrors mode="Off"/>, My service works well BUT when i set it ON , it shows me the error !!!!!!!!!!!!!!!!!
my face is like a Question mark NOW :-) .
why?!!
thanks Again