2017-10-20 78 views
1

因爲有時我的系統,不但有GET型和擊中POST類型有時。 如果我用[HttpPost]屬性修飾我的方法,我應該使用JsonRequestBehavior.AllowGet返回Json結果嗎?我們可以使用帶有[HttpPost]屬性的JsonRequestBehavior.AllowGet嗎?

如:

 [HttpPost, ValidateAntiForgeryToken, Authorize] 
     public ActionResult AssociatedDevices(long id, [DataSourceRequest] DataSourceRequest request) 
     { 
      var dataParameters = request.ToDataParameters(); 
      var deviceSetLogic = new DeviceSetLogic(); 
      var associatedDevices = deviceSetLogic.GetAssociatedDevicesByDeviceSetId(id, dataParameters); 

      var result = new DataSourceResult() 
      { 
       Data = associatedDevices, 
       Total = Convert.ToInt32(dataParameters.TotalRecordCount) 
      }; 

      return Json(result, JsonRequestBehavior.AllowGet); 
     } 

如果我寫上面一樣在PROD環境會造成任何問題?請指教。

+3

裝飾如果你的方法裝飾有'HttpPost'屬性,你並不需要使用'JsonRequestBehavior.AllowGet'。爲什麼不在運送到PROD之前在本地進行測試? – Shyju

+0

以來,在督促環境中,我們越來越像 「System.Web.HttpException 公共行動方法‘AssociatedDevices AdminSite.Web.Controllers.EntitlementsSearchController’是不是在控制器中發現‘異常的’。」 但我們無法在localhost中重現此錯誤。某處系統正在觸碰GET類型而不是POST,這會導致此錯誤。 –

回答

1

JsonRequestBehavior.AllowGet參數添加到您的返回Json沒有用處,因爲您的方法使用[HttpPost]修飾,所以無法使用GET動詞調用它。

你說,有時你的系統「使用get打,有時用後」,但如果你嘗試使用一個GET請求來調用這個方法路由系統將最有可能獲得404

沒有辦法這方法回答GET請求,因此添加JsonRequestBehavior.AllowGet僅使代碼不太清晰。

如果你的動作必須使用POST和GET動詞來reachabe,應該用[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)][AcceptVerbs("Get", "Post")]

+0

好了,如果我AcceptVerbs裝飾(HttpVerbs.Get | HttpVerbs.Post)],然後我可以用這個JsonRequestBehavior.AllowGet在JSON結果? –

+0

我已經更新了我的答案。回答你的評論:是的 – Wndrr

+0

謝謝你的upvote。如果您的問題得到解答,請將其標記爲如此。 – Wndrr

相關問題