Reputation: 5234
I made a nice web page with jquery ajax calls, but noticed that Page_Load is not called for a jquery ajax POST request. I need the Page_Load call for access to the name of the logged in user, to store in the database with data received through POST, as the static WebMethod in code-behind has no access to the Page object.
My question: is it normal that Page_Load is not called on a POST request? If so, how to get the Page object for the user name, without using Page_Load?
In comment on this question I read: Support for WebMethod is no longer maintained. You should consider switching over to Web API, as it's well supported (and simpler!)
But googling did not really result in info about Web API, as this is a very general search term. Any info on this?
Update:
My problem is solved by using HttpContext in the static WebMethod, as commented below, so I do not need Page_Load.
Upvotes: 0
Views: 155
Reputation: 5778
There are two ways to do that
In your javascript add a variable which set current username
and pass this as a parameter to POST
method.
var usrName = "@HttpContext.Current.User.Identity.Name";
HttpContext is also available in the static WebMethod in code-behind so you can directly achieve the same thing using this as well
Upvotes: 1