I'm developing a web app with asp.net mvc 2. This app, has a controller with some asynchronous operations that return json or ajax... I call it by jquery and works fine!
My script is on the MasterPage, so this operations can be called by any View that inherits from this MasterPage.
My question is, How could I know ... what is the controller and action that are requesting the asynchronous operation?
I tried this:
if (this.RouteData.Values["controller"] == "Product" && this.RouteData.Values["action"] == "Index") {
}
but this get the current action (my assync action... or... "THIS" action!), I want the request.
I saw it because, if the request came from Home/Index or Home/Contact or Customer/Index or Product/Index my result of json can be diferent, so, I'd like to test what's the controller and action.
thanks!
---- Edited
It's a system of job monitoring of my customer. I do something like this:
//every second I get info in my assync action:
$(document).ready(function () {
var interval = window.setInterval(GetJobs, 1000);
});
function GetJobs() {
$.getJSON('<%=Url.Action("Index", "AssyncJob", new { area = "Admin"}) %>', function (r) {
/// ----------- Info in MasterPage (All views need it) ------------ //
// setup the time of server...
$("#time").html(r.time);
// setup the jobs are running... (
$("#running").html("");
if (r.jobcount == 1)
$("#running").html("There is one job running!");
else if(r.jobcount > 1)
$("#running").html(r.jobcount + " jobs running!");
/// ----------- Info in Home/Index ------------ //
if ($("#jobstoped")) { $("#jobstoped").html(r.jobstoped); }
// get a list of jobs... (in my action this info is in Cache)
if (r.jobs != null) {
$(r.jobs).each(function () {
if ($("#job" + this.id)) {
if (this.IsRunning) {
if (!$("#job" + this.id).hasClass("running")) {
$("#job" + this.id).addClass("running");
}
}
else if (this.IsStoped) {
if (!$("#job" + this.id).hasClass("stoped")) {
$("#job" + this.id).addClass("stoped");
}
}
else if (this.IsEnding) {
if (!$("#job" + this.id).hasClass("finished")) {
$("#job" + this.id).addClass("finished");
}
}
// --- there is a lot of info and rules that I fill for each job in list
}
});
}
});
}
I return some infos and works fine but I need to return the list of jobs only on Index action at Home controller, because this... I need to know what's the route are requesting the assync action ... to improve performace and avoid unnecessary information!
Well if you can help my... I would greatly appreciate it! =D
Thanks again!
If your JSON is going to be different depending on which route you have why not separate out the different routes into different actions, then you wouldn't have to do the check you are asking. It would make the code a lot cleaner and easier to read than having a bunch of if-else blocks in one action to determine which ActionResult to return to the view.