Asp.net-Mvc-4

Web API ActionFilterAttribute 可以訪問模型嗎?

  • December 27, 2015

我希望能夠在 web api ActionFilter 中進行一些權限檢查,所以我需要能夠提取對象 ID。因為我可以訪問 RouteData,所以我可以在 GET 上執行此操作,但是是否可以訪問 PUT\POST 的操作過濾器中的 searlized viewModel 對象?

public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
   {
       if (actionContext == null)
       {
           throw new ArgumentNullException("actionContext");
       }

      //Get ID from searlized object and check permissions for a POST\PUT? 
   }

您是否嘗試過 ActionArguments 屬性?

您可以通過此屬性訪問序列化模型:

actionContext.Response.Content

不過,確定要從該屬性反序列化的內容可能需要一些工作。Web API 文件現在非常稀少,但這裡是官方文件。Response.Content類型是這樣的HttpReponseMessage,您應該通過搜尋找到更多反序列化的詳細資訊。

引用自:https://stackoverflow.com/questions/12697766