2010-12-02 59 views
1

我正在构建一个应用程序,它将关闭服务器,ping该服务器,并在服务器已关闭时通知用户。如果它没有成功关闭,它允许用户“再试一次”。我可以让应用程序第一次正确处理逻辑,但是当我实现“重试”功能时,它不会重新处理关机操作。我认为它必须与缓存有关,但我不知道该从哪里开始寻找。有任何想法吗?url.action,jquery和mvc

这里是我的代码:

控制器

//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") %> 
     <% } %> 
    <% } %> 

回答

3

我相信缓存实际上是由jQuery完成的。将$ .get更改为$ .post并查看它是否有效。如果确实如此,你仍然可以使用$不用彷徨,但你需要设置的选项:在了解的过程中

cache: false 

HTH, 布赖恩

+1

+1 Internet Explorer特别喜欢缓存GET请求。最简单的方法是使用POST代替。 HTTP规范说GET只能用于没有任何效果的请求。由于这个请求应该改变服务器的状态,它肯定应该是一个POST。 – StriplingWarrior 2010-12-02 20:55:06

0

尝试增加:

[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> 
+0

感谢这个信息,以及...非常有帮助 – bcahill 2010-12-02 21:30:00