2017-10-15 83 views
0

我用這個代碼顯示的圖如何防止在asp.net mvc中兩次調用一個動作?

namespace TravelEnterAdminTemplate.Controllers 
{ 
    public class ArtPlacesController : Controller 
    { 
     private TravelEnterDBEntities1 db = new TravelEnterDBEntities1(); 
     private Models.Utility.ErrorLog errorlog = new Models.Utility.ErrorLog(); 


     public async Task<ActionResult> Index() 
     { 
      try 
      { 
       var artPlaces = db.ArtPlaces.Include(a => a.AspNetUser).Include(a => a.City); 
       return View(await artPlaces.ToListAsync()); 
      } 
      catch (Exception e) 
      { 
       errorlog.Error("IndexArtPlaces", "25", e.Source.ToString(), e.Message); 
       return View("Error", TempData["Error"]); 
      } 

     } 

    } 
} 

我設置斷點進入指數的行動,當調試應用程序,我發現,這兩次行動呼籲。我複製網址並將其粘貼到地址欄中,然後看到此操作兩次都會調用。 我嘗試檢測到java腳本語句完成了這項工作,並將下面的代碼放入頁面的頭部,但在再次執行動作之前不會提醒我。

window.onbeforeunload = function(e) { 
    return 'Dialog text here.'; 
}; 

我在控制檯中運行此代碼,並嘗試重新加載頁面時得到警報。 我認爲頁面不會重新載入,但行動再次調用。我認爲這個問題是後端代碼。

RouteConfig:

namespace TravelEnterAdminTemplate 
{ 
    public class RouteConfig 
    { 
     public static void RegisterRoutes(RouteCollection routes) 
     { 
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

      routes.MapRoute(
       name: "Default", 
       url: "{controller}/{action}/{id}", 
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
      ); 
     } 
    } 
} 

如何解決這一問題?

+2

檢查客戶端代碼,它可能會調用您的操作方法兩次。 – vmeln

+0

你可以顯示HTML&尤其是正在打電話給控制器的Jquery –

+0

我直接使用url。而這不會發生時調用與AJAX的行動 – programmer138200

回答

0

在我的網站佈局退出<link rel="shortcut icon" href="#" type="image/png">href="#"使召回行動。 我刪除了這個並解決了問題。

+0

會發生什麼,應該是評論而不是回答 –

相關問題