ajax ashx aspx,asp.net - .NET AJAX Calls to ASMX or ASPX or ASHX? - Stack Overflow

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值