作为对象传入时,如何获取ASP.NET MVC中模型对象的属性值? [英] How do I get the values of the properties of a model object in ASP.NET MVC when passed in as an object?

查看:427
本文介绍了作为对象传入时,如何获取ASP.NET MVC中模型对象的属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够将可能传递给LogEvent函数的各种对象的属性写入日志.我在ASP.NET MVC应用程序中定义了各种对象,例如城镇,纳税人,TaxedProperty,TaxAccount等.我想写一些传递给我的对象的日志以及其他信息.

I would like to be able to write to a log the properties of various objects that I might pass to my LogEvent function. I have various objects defined, such as Town, Taxpayer, TaxedProperty, TaxAccount, etc. in my ASP.NET MVC application. I would like to write to some log passing in my object along with a bunch of other information.

我正在将其写入数据库表,并且有一个大文本字段,在其中可以插入发生坏事时正在处理的对象的表示形式.也许用户正在尝试编辑他们无权访问的纳税人.因此,我将写此用户试图访问此纳税人的日志,并执行以下操作:

I am writing this to a database table and I have a large text field in which I can insert a representation of the object that was being processed when a bad thing happened. Perhaps the user was trying to edit a taxpayer they did not have access to. So, I would write to the log that this user tried to access this taxpayer and I would do this:

kurantLogic.LogEvent(userName, null, null, Request.HttpMethod, "Taxpayers/Edit: Access denied because user does not have access to the taxpayer with ID=" + taxpayer.ID, false, taxpayer);

最后一个参数是纳税人对象,但也可以是其他对象,例如城镇.这是另一个示例:

the last argument is the taxpayer object, but it could be some other object, such as a town. Here is another example:

kurantLogic.LogEvent(userName, null, null, Request.HttpMethod, "Towns/SetUpYourTown: Access denied because user does not have access to the town with ID=" + town.ID, false, town);

或者下面是记录好东西的示例:

or here is an example of logging a good thing:

kurantLogic.LogEvent(userName, null, null, Request.HttpMethod, "Towns/SetUpYourTown: Success, Town edit saved = " + town.Name, false, town);

我想列出城镇或纳税人或传入的任何对象中的字段,而不必对可能传入的事物的所有属性进行硬编码.

I would like to list out the fields in the town or taxpayer or whatever object is passed in without having to hard code all the properties of the thing I might pass in .

这是我开始的代码:

public void LogEvent(string userName, int? userID, int? townID, string httpVerb, string description, bool isError, Object obj)
        {
            JsonSerializerSettings settings = new JsonSerializerSettings();
            settings.MaxDepth = 1;
            settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            settings.NullValueHandling = NullValueHandling.Ignore;
            string bigText = JsonConvert.SerializeObject(obj, settings);
            LogEvent(userName, userID, townID, httpVerb, description, isError, bigText);
        }

除了JSON转换器写出了对象包含的所有东西的集合之外,这种工作方式是可行的,而我找不到抑制它的方法.因此,如果有一种方法可以使JSON.SerializeObject仅包括字符串,整数,小数,布尔值等属性,而不包括其他对象,那将是一个很好的解决方案.但是我以为我可以使用反射来做到这一点,但是遇到了麻烦.

That kind of worked except that the JSON converter wrote out all the collections of things that the object contained, and I cannot find a way to suppress that. So, if there is a way to make JSON.SerializeObject include only the properties that are strings, ints, decimals, booleans, and not other objects, that would be a good solution. But I thought I could use Reflection to do it, but am having trouble with that.

我的Taxpayer类具有与之相关联的TaxedProperties之类的东西,而我没有记录.这就是我现在得到的.在漫长的JSON序列化结束时,您可以看到列出的简单属性.

My Taxpayer class has things like TaxedProperties that are associated with it, and I don't those logged. This is what I am getting now. At the end of this long JSON serialization you can see the simple properties listed.

好吧,我无法发布JSON,因为StackOverflow认为它看起来像垃圾邮件!希望您能提供帮助.

Well, I could not post the JSON because StackOverflow thinks it looks like spam! I hope you can help.

我只想记录对象的标量"属性而无需进行硬编码.我认为使用JSON或Reflection都应该可行,但是我很沮丧.

I just want the "scalar" properties of the object logged without hard-coding. I would think that should be possible with either JSON or Reflection, but I am stumped.

推荐答案

您可以使用 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆