I’m still using webforms. At the moment I’m not planning to use MVS, despite this I need / I would like to use Ajax with jQuery but to do this I need to create a static [WebMethod] that acts as static, no session nor common things can be used, such getting things from DB using user data stored in session, at least easy way session[]=.
[WebMethod]
public static void setAsRead(string rowid){}
(In the previous code I need to store in the page, as hidden field, the rowid of the table row which I need its data)
So I wonder whether placing a lot of pagemethods to do so is “normal” or we have to avoid this approach. I personally think that it’s ugly to see this because the page nature, it’s a webform, a page, not a webservice stack.
So what do you think? Are there security issues? Do I have to avoid ajax with webforms and thinking to migrate to MVC?
There’s nothing wrong with PageMethods in classic WebForms in terms of security or performance. They are much better than the UpdatePanels as you have full control over the data that is being exchanged between the client and the server. The benefit of using PageMethods compared to raw IHttpHandler is that the framework will take care of the JSON serialization and the plumbing code for you.
So you could PageMethods until you decide to upgrade to ASP.NET MVC, ASP.NET Web API or ServiceStack where you could expose a REST API.
No, there’s absolutely nothing wrong with doing AJAX in WebForms. People have been using it much before ASP.NET MVC ever existed. But if you are starting a new project you might really consider whether it wouldn’t be worth the effort to migrate to ASP.NET MVC. This will very much depend on the specific requirements of your project, the existing codebase, … Make sure you evaluate all possibilities really well before starting the migration process.