I'm struggling to understand the execution order in MVC architecture.
In my code, I am overriding initialization in the following way:
public class HomeController: Controller {
protected override void Initialize(RequestContext requestContext)
{
var myVar = "AValue";
// running some internal security initialization
}
Then, I have a series of 10 Ajax calls with deferred promises in my javascript.
Setting them up and running onload with $when function.
Now, in a different location than before:
JavaScript file snippet:
this.BeginMyAjaxQuery = function() {
methodName: MyAjaxQuery
};
In an entirely different Controller that inherits from my HomeController:
public JsonResult MyAjaxQuery()
{
return Perform(() =>
{
return ThisQueryWithMyParameter(AValue);
});
}
"Perform" is a wrapper successfully used to create JsonResults.
The issue lies in the behavior of MVC architecture - when I load the URL, the AJAX calls run all the way through on their own thread before my HomeController initialization is complete. As a result, my variables are not set properly leading to incorrect results, although inconsistently.
How is it possible for the ajax calls to bypass my controller initialization and directly access controller methods?
Adding the same code to a constructor in my HomeController also gives erratic results, adding to my confusion.
Additional info mentioned: Using Partial Views & KnockoutJS for front end delivery.