smwikipedia
smwikipedia

Reputation: 64205

"WebMethod" attribute in the aspx and asmx files, difference?

I noticed in some code that [WebMethod] attribute is used in the code-behind file of an aspx page.

But as I remember it, this attribute is used to expose Web Service and it is often seen in asmx file.

So what's the difference between these 2 usages?

Thanks.

Upvotes: 1

Views: 5545

Answers (2)

saarthak
saarthak

Reputation: 1014

Webmethods in code behind are used for AJAX calls. If you are using jquery or similar and you need to implement an ajax functionality on your page then you will have to define you method with WebMethod attribute and have to make it public static. Then only it will work.

WebMethod's concept I feel has been taken from web services. Since asp.net didn't have any defined way of handling ajax requests to method of page behind, they have extended this feature to be used for code behind methods.

Keep a watch that being public static methods you may not be able to use your page class' internal properties here. so, you will need to deal with that.

Upvotes: 1

misha
misha

Reputation: 2889

If that method is also static, you can call that method via javascript/ajax, without a full page postback. Note that your ScriptManager needs to have the EnablePageMethods property set to true.

Upvotes: 2

Related Questions