40

What is the most efficient way of calling some business logic from javascript on the client side using AJAX? It looks like you can call a [WebMethod] on an aspx directly from javascript (in my case I'm using JQuery to help out) OR you can call a .asmx directly. Which call incurs less overhead? What is the best practice?

Also, what does the [ScriptService] attribute do on a class? I have never used this before on my .aspx [WebMethod] methods and everything seems to be working fine.

I'm hoping this is a purely objective question. Thanks in advance!

3 Answers 3

31

The ScriptService stuff in my opinion is a hidden gem in asp.net. Calls to the script service do not passback form data + viewstate, they are lean, fast JSON payloads.

Heres the best part, ASP.NET3.5's scriptmanager can do most of the work for you regarding generating a JS method for you to call and also setting up any JS classes needed.

A simple example for fetching details for a "Person", assuming Person is a C# class.

In PersonService.asmx:

namespace MyProj.Services {
  [System.Web.Script.Services.ScriptService]
  [System.Web.Script.Services.GenerateScriptType(typeof(Person))] 
  public class PersonService : System.Web.Services.WebService
  {
    [WebMethod, ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public Person GetPersonDetails(int id)
    { 
       /* return Logic here */
    }
  }
}

In DetailsPage.aspx

<asp:ScriptManager ID="ScriptManager1" runat="server">
 <Services>
  <asp:ServiceReference Path="~/Services/PersonService.asmx" />
 </Services>
</asp:ScriptManager>

By using a setup like this, you won't even need the help of JQuery to call the service and get back a JS version of your C# Person class, .net does that all for you. An example of using this service from JS would be:

MyProj.Services.PersonService.GetPersonDetails(id, _onDetailsCallbackSuccess, _requestFailed, null);

_onDetailsCallbackSuccess: function(result, userContext, methodName) {
 //all persons properties are now intact and available
 document.getElementById('txtFirstname').value = result.Firtname;
}

Anyway, it would be more then worth looking into the ASP.NET Ajax ScriptService stuff. Even if you decide not to use it this time it's a pretty wicked feature.

Links

3
  • Thanks, your answer was really helpful . I marked the other one as the answer since he responded first but i voted up your response since it will be useful going forward Mar 24, 2009 at 16:28
  • 2
    @jake: You should mark the most helpfull answer as the answer. We are rewarding great answers here, not quick typists.
    – Espo
    May 18, 2012 at 9:24
  • The first link (www.jankoatwarpspeed.com) returns Page not found :( It may have been removed inbetween...
    – Mochi
    Jan 9, 2015 at 12:56
17

If server-side overhead is all you care about, then sending a simple GET with a query string to an ASHX would probably be it. It's definitely preferable to an .ASPX, which is going to go through a page life-cycle that you don't need.

The advantage of an ASMX web method is that it's built on a standard that can be called by other technologies easily (supports discovery). With some documentation, your .ASHX will be just as easy to call, though.

4
  • Can you call a specific [WebMethod] on a .ASHX doing a GET? Mar 23, 2009 at 17:53
  • 2
    That's not what it's for. An .ASHX is going to have to crack the querystring and branch to the method itself. It's more programming work than an ASMX, but very low-overhead while running -- pretty much the smallest you can get while still using ASP.NET.
    – Lou Franco
    Mar 23, 2009 at 19:30
  • So an ashx is really an HTTPRequestHandler that doesn't go through a page lifecycle? That will be handy for other things but I'll probably want to avoid using that in this particular case as I may have 10+ [WebMethods] which would really involve using a giant switch statement... Mar 24, 2009 at 12:27
  • 6
    You could have 10+ ashx pages instead of a giant switch statement
    – Matt Frear
    Dec 21, 2009 at 4:52
14

The way you worded your question, I believe you're considering the difference between an ASMX "ScriptService", an ASHX HttpHandler, and an ASP.NET AJAX "PageMethod" (not an actual ASPX Page).

If this is the case, they are all very similar. The ASP.NET AJAX PageMethods are, for all intents and purposes, identical to ASMX ScriptServices. They don't create an instance of the Page, so they don't incur the overhead normally associated with ASPX pages.

Unless you're talking about saturating the server with requests, you'll probably find that all three perform comparably.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.