2014-11-20 55 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); 
} 

希望这有助于。如果还有其他问题,请告诉我,我会尽力帮助。