【问题标题】:url.action, jquery, and mvcurl.action、jquery 和 mvc
【发布时间】:2011-05-19 09:15:16
【问题描述】:

我正在构建一个应用程序,它将关闭服务器,ping 该服务器,并在服务器已关闭时通知用户。如果它没有成功关闭,它允许用户“再试一次”。我可以让应用程序第一次正确处理逻辑,但是当我实现“重试”功能时,它不会重新处理关闭操作。我认为它一定与缓存有关,但我对它的了解还不够,不知道从哪里开始寻找。有什么想法吗?

这是我的代码:

控制器

    //URL: Build/Progress  
    public ActionResult Progress()  
    {  
        return View();  
    }  

////URL: Build/MoveDevice  
public ActionResult ShutdownStatus()  
{  
    ImageInfo ImageInfo = new ImageInfo();  
    FarmInfo FarmInfo = new FarmInfo();  
    ProgressModel ProgressModel = new ProgressModel();  

    if ((Session["ShutdownStatus"] as string) == "Success")  
    {  
        ProgressModel.ShutdownStatus = 1;  
        return View(ProgressModel);  
    }  
    else  
    {  
        ImageInfo = svcImage.GetImageInfoByImageId(Session["ImageName"].ToString());  
        string Farm = ImageInfo.FarmId.ToString();  
        FarmInfo = svcFarm.GetFarmByFarmId(Farm);  
        svcImageSvc.ShutdownDevice(Session["Username"].ToString(), Session["Password"].ToString(), Session["Domain"].ToString(),  
            FarmInfo.farmName, ImageInfo.Device, ImageInfo.ImageHistory.Status, ImageInfo.PVSHost, ImageInfo.PVSAccount);  

        ProgressModel.ShutdownStatus = svcImageSvc.PingDevice(ImageInfo.Device);  
        return View(ProgressModel);  
    }  
}

视图 (Progress.aspx)

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>  

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">  
    Progress  
</asp:Content>  

<asp:Content ID="Content5" ContentPlaceHolderID="HeadContent" runat="server">  

</asp:Content>  

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">  
    <h1>Progress</h1>  
    <hr />  

    <!-- Shutdown Status -->  
    <div class="panel" id="panel1"><img src="../../Assets/Images/load.gif" /></div>  
    <script type="text/javascript">  
        $.get('<%= Url.Action("ShutdownStatus", "Build", null) %>',  
        function (data) {  
            $('#panel1').replaceWith(data);  
        });  
    </script>  

</asp:Content>  

<asp:Content ID="Content3" ContentPlaceHolderID="HeaderContent" runat="server">  
</asp:Content>  

<asp:Content ID="Content4" ContentPlaceHolderID="FooterContent" runat="server">  
</asp:Content>

查看 (ShutdownStatus.asxc)

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<PvsUtil.App.Core.Models.ProgressModel>" %>  

    <% using (Html.BeginForm())  
       { %>  
        <% if (Model.ShutdownStatus == 1)  
           { %>  
           <%: Session["ShutdownStatus"] = "Success" %>  
            <p>  
                Server shutdown succesfully  
            </p>  
            <% } %>  

        <% if (Model.ShutdownStatus == 2)  
            { %>  
            <p>Server did not shutdown within 1 minute.  Please check the server and try again.</p>  
            <%: Html.ActionLink("Try Again", "Progress", "Build") %>  
            <% } %>  

        <% if (Model.ShutdownStatus == 3)  
        { %>  
        <p>An error has occurred.  Please check the server and try again.</p>  
        <%: Html.ActionLink("Try Again", "Progress", "Build") %>  
        <% } %>  
     <% } %>  

【问题讨论】:

    标签: jquery asp.net-mvc url.action


    【解决方案1】:

    我相信缓存实际上是由 jQuery 完成的。将 $.get 更改为 $.post 并查看它是否有效。如果是这样,您仍然可以使用 $.get,但您需要设置选项:

    cache: false
    

    HTH, 布赖恩

    【讨论】:

    • +1 Internet Explorer 特别喜欢缓存 GET 请求。最简单的解决方法通常是使用 POST 代替。 HTTP 规范说 GET 应该只用于没有效果的请求。由于这个请求应该改变服务器的状态,所以它肯定应该是一个 POST。
    • 是啊,想了想,这应该是个POST。我一般使用 GET 来查询信息,如果我正在做任何预期会改变网站(或系统)状态的事情,那么我倾向于使用 POST。
    • 甜!帖子成功了。多谢你们。你是对的。 Chrome 和 Firefox 运行良好。 IE7 给我带来了问题
    【解决方案2】:

    尝试添加:

    [OutputCache( Location = OutputCacheLocation.None )]
    

    到您的控制器(或您不想缓存的每个操作)。

    您想做的另一件事是将您的 javascript 包装在 document.ready() 处理程序中。这并不是绝对必要的,因为您的脚本出现在它引用的元素之后,但它仍然是一个很好的做法——您也可以将它移动到结束 body 标记的正上方,但这更像是一种优化。

    <script type="text/javascript">
      $(function() { // execute on document ready   
          $.get('<%= Url.Action("ShutdownStatus", "Build", null) %>',   
          function (data) {   
              $('#panel1').replaceWith(data);   
          });
      });  
    </script>
    

    【讨论】:

    • 也感谢您提供此信息...对了解流程非常有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    • 2011-08-21
    • 1970-01-01
    • 2020-12-29
    • 2010-12-18
    • 2013-02-23
    相关资源
    最近更新 更多