代码之家  ›  专栏  ›  技术社区  ›  Felipe Oriani

从请求中获取路由数据

  •  4
  • Felipe Oriani  · 技术社区  · 15 年前

    我正在用ASP.NET MVC 2开发一个Web应用程序。这个应用程序有一个控制器,它有一些异步操作返回json或ajax…我叫它jquery,工作得很好!

    我的脚本在母版页上,因此任何继承此母版页的视图都可以调用此操作。

    我的问题是,我怎么知道…请求异步操作的控制器和操作是什么?

    我试过这个:

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

    但这得到了当前的操作(我的同步操作…或者……”这个“行动!”,我想要这个请求。

    我看到它是因为,如果请求来自home/index或home/contact或customer/index或product/index,我的json结果可能会有所不同,所以,我想测试什么是控制器和操作。

    谢谢!

    编辑

    这是一个对我的客户进行工作监控的系统。我会这样做:

    //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
    
    
                        }
                    });
    
                }
            });
    

    }

    我返回一些信息并且工作正常,但是我只需要在主控制器的索引操作上返回作业列表,因为这…我需要知道路由在请求同步操作…提高性能,避免不必要的信息!

    如果你能帮我…我会非常感激的!= D

    再次感谢!

    1 回复  |  直到 15 年前
        1
  •  3
  •   amurra    15 年前

    如果您的JSON将根据您拥有的路由而有所不同,那么为什么不将不同的路由分为不同的操作,那么您就不必执行您要求的检查。它将使代码比在一个操作中使用一组if-else块来确定返回到视图的actionresult更干净、更容易阅读。