gpt4 book ai didi

asp.net-mvc - 从请求中获取路由数据

转载 作者:行者123 更新时间:2023-12-01 02:53:09 26 4
gpt4 key购买 nike

我正在用 asp.net mvc 2 开发一个 web 应用程序。这个应用程序有一个 Controller ,有一些异步操作,返回 json 或 ajax ......我通过 jquery 调用它并且工作正常!

我的脚本在 MasterPage 上,因此从该 MasterPage 继承的任何 View 都可以调用此操作。

我的问题是,我怎么知道......请求异步操作的 Controller 和 Action 是什么?

我试过这个:

if (this.RouteData.Values["controller"] == "Product" && this.RouteData.Values["action"] == "Index") {    
}

但这会获得当前操作(我的异步操作...或...“此”操作!),我想要请求。

我看到它是因为,如果请求来自 Home/Index 或 Home/Contact 或 Customer/Index 或 Product/Index,我的 json 结果可能会有所不同,因此,我想测试 Controller 和操作是什么。

谢谢!

---- 已编辑

这是我的客户的工作监控系统。我做这样的事情:
//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


}
});

}
});

}

我返回了一些信息并且工作正常,但我只需要在 Home Controller 的 Index 操作上返回作业列表,因为这......我需要知道请求异步操作的路由......以提高性能并避免不必要的信息!

好吧,如果你能帮助我的......我将不胜感激! =D

再次感谢!

最佳答案

如果您的 JSON 将根据您拥有的路线而有所不同,为什么不将不同的路线分成不同的 Action ,那么您就不必进行您所要求的检查。与在一个 Action 中使用一堆 if-else 块来确定要返回到 View 的 ActionResult 相比,它会使代码更清晰、更易于阅读。

关于asp.net-mvc - 从请求中获取路由数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4096025/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com