2014-11-20 51 views
0

我想將每個操作方法參數名稱及其 相應的值記錄在數據庫中作爲鍵值對。作爲 的一部分,我使用OnActionExecuting ActionFilterAttribute,因爲它將會是正確的地方(OnActionExecuting方法將調用 所有控制器動作方法調用)來獲取Action Executing上下文。Asp .Net MVC正在執行操作 - 獲取用戶定義類型的操作參數值在執行操作時的值

我得到.Net類型(string,int,bool)的值。但我 無法獲取用戶定義類型(自定義類型)的值。 (例如:登錄模式)。我的模型也可能有一些其他嵌套用戶定義的類型。

我試圖獲取用戶定義類型的值,但我 獲取唯一的類名字符串。我希望我們可以在 中做反思。

請問任何人協助解決問題。因爲我是新的 來反思。這對我有幫助。提前致謝。 我需要在OnActionExecuting中獲取這些類型的名稱和值。

public override void OnActionExecuting(ActionExecutingContext filterContext) 
{ 
    ActionParameter = new SerializableDictionary<string,string>(); 

    if(filterContext.ActionParameter != null) 
    { 
     foreach(var paramter in filterContext.ActionParameter) 
     { 
      //able to get returnUrl value 
      //unable to get model values 

      ActionParameter.Add(paramter.Key, paramter.Value); 
     } 
    } 

} 

public ActionResult Login(LoginModel model, string returnUrl) 
{ 
    return View(model); 
} 

用戶定義類型

public class LoginModel 
{ 
    public string UserName {get;set;} 

    public string Password {get;set;} 

    //User defined type 

    public UserRequestBase Request {get;set;} 

} 

//User defined type 
public class UserRequestBase 
{ 
    public string ApplicationName {get;set;} 
} 

我能夠得到OnActionExecuting的RETURNURL(登錄方法PARAM)的值,而不是模型(登錄方法PARAM)。我能夠看到值,但不知道如何訪問它,儘管我無法獲取它,但我需要泛型,因爲我在控制器中有20個方法,所以我不僅可以使用LoginModel。

+0

你可能想看看這個:http://stackoverflow.com/questions/9758051/get-type-using-reflection – wahwahwah 2014-11-20 20:02:38

+0

正如在鏈接中提到的,我們需要提供「Item」作爲鍵getProperty,但是在這裏我不知道屬性名稱,因爲它對於每個操作方法可能會有所不同。 。actualData.getType()的getProperty( 「項目」)PropertyType.Name。 – Prem 2014-11-20 20:08:24

+0

請任何人都可以幫忙嗎? – Prem 2014-11-21 07:43:03

回答

1

這個答案不是究竟是你想要什麼 - 基於你的問題 - 但我認爲它會更好的完成任務。快速一邊......

在這種情況下反射和嵌套類玩弄,導致一些SO(一個propos?)爲我的錯誤...

所以,更好的路徑,也許?我沒有試圖從'context.ActionParameters'中獲取/轉換屬性名稱,值(類型?),而是讓Json序列化爲我完成工作要容易得多。然後你可以堅持Json對象,然後反序列化...很容易。

總之,這裏的代碼:

using Newtonsoft.Json; // <-- or some other serialization entity 
//... 
public class LogActions : ActionFilterAttribute, IActionFilter 
    { 

     // Using the example -- LoginModel, UserRequestBase objects and Login controller... 

     void IActionFilter.OnActionExecuting(ActionExecutingContext context) 
     { 
      var param = (Dictionary<String, Object>)context.ActionParameters; 

      foreach (var item in param.Values) 
      { 
       string itemName = item.GetType().Name.ToString(); 
       string itemToJson = JsonConvert.SerializeObject(item); 


       // Save JsonObject along with whatever other values you need (route, etc) 
      } 
     } 

    } 

然後當你檢索你只需要反序列化數據庫中的JSON對象/投放。

LoginModel model = (LoginModel)JsonConvert.DeserializeObject(itemToJson, typeof(LoginModel)); 

從例如:

public class LoginModel 
{ 
    public string UserName {get;set;} 

    public string Password {get;set;} 

    //User defined type 

    public UserRequestBase Request {get;set;} 

} 

//User defined type 
public class UserRequestBase 
{ 
    public string ApplicationName {get;set;} 
} 

控制器示例中使用:

public ActionResult Login(LoginModel model, string returnUrl) 
{ 
    return View(model); 
} 

希望這有助於。如果還有其他問題,請告訴我,我會盡力幫助。