views:

36

answers:

3

Hi,

I'm new to that subject in ASP .NET, however I had experience in PHP - where whole idea was preety straightfowrad: create page like something.php which returns pure JSON, and call it using $.post (for instance).

Now , in ASP, I walked into another approach (using Web Services + ScriptService attribute + MS library for proxy generation). I realy don't find it useful as I keep my hands in jquery for a long time and I'm not keen on learning yet another javascript framework library...

Bottom line: what it the best approach (less code, fastest) of creating AJAX calls using jquery to ASP .NET application hosted in IIS. For the time being, I've been creating empty pages (like CheckSession.aspx), with all code in Page_Load. Method wrote response using Response.Write and then ended it using Response.End. What bothers me, is:

  • as ASP .NET isn't lightweight, I'm concerned about performance of this solution (all page life cycle etc...). Perhaps there is an easier/faster way to do it?
  • that I still have codebehind cs file + aspx file (I would like to have one file, less code, less files, less hassle).
  • in Response.Write as a parameter I put just a manually concatenated string. What if I would like to use objects and serialize it like Response.Write(obj.toJSON()) - is it possible?

I would appreciate some guidelines, links.

Thanks, Paweł

A: 

Personally, I find ASP.NET MVC is a perfect partner to jQuery, particularly JSON, and alleviates many of the "heavy-weight" concerns that many people have about traditional Web Forms.

Returning JSON data to jQuery is as simple as returning a JsonResult of a plain-ol' CLR object, like follows:

public ActionResult GetData() {
    var domainObject = Repository.Load();

    return Json(domainObject);
}

That's really all there is to returning most objects straight down to jQuery, and in my experience it works perfectly well with the standard jQuery JSON functionality.

You don't have to deal with "views" that are really just JSONifying data, manually concactenating strings or any of that other manual cruft that you can run into with Web forms.

Ryan Brunner
thanks, I'll keep that in mind. Unfortunatelly my current project uses ASP .NET 2.0, not MVC version.
dragonfly
A: 

as ASP .NET isn't lightweight, I'm concerned about performance of this solution (all page life cycle etc...). Perhaps there is an easier/faster way to do it?

This site (stackoverflow) is built using ASP .NET, and I think it has a pretty good performance.

Daniel Moura
dragonfly
A: 

It sounds like ASMX web services fit your requirements. ASMX calls do not go through the full page lifecycle that ASPX calls do, so they will generally perform slightly better than similar functionality implemented as an ASPX page. They are defined strictly within a .cs code file (no .ASPX / .ASPX.CS code-behind relationship). They can be configured to automatically serialize your returned objects into JSON, so you won't be doing any Response.Write calls. If you use the "EnableSession = true" attribute, you can use the same session as your ASPX pages, which you may find convenient.

This stackoverflow discussion should give you pretty much everything you need to know. For that matter, searching stackoverflow for ASMX and JSON should give you lots more good info.

WCF is another option to consider: MS is moving away from ASMX and toward WCF for web service development. However, if you're locked into .NET 2.0, WCF may not be an option for you. Also, in my opinion, I find ASMX services faster and easier to develop, if you don't need the additional functionality and flexibility of WCF.

mikemanne