2010-12-02 64 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