.net 脚本 Web 服务:__type 属性
我正在使用从客户端脚本调用的 .net 脚本服务,它运行得非常好。
唯一的问题是 - 它为每个返回的对象生成一个“__type”属性,这是我不想要或不需要的。
我在网上看到了一些关于此问题的帖子,据我所知,只有“解决方法”:
有些人建议将返回类型的无参数 c'tor 隐藏为“内部受保护',
其他人建议不要使用[ScriptMethod]标签,而是手动JSONfy结果并返回一个字符串。
我想知道是否有另一种更好的解决方案。顺便问一下 - 这个属性到底有什么用?
我附上了服务方法和生成的 JSON。
方法:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
public IEnumerable<EmployeePO> GetEmployeesForDepartment(int DepartmentId)
{
return new AdministrationController().GetEmployeesForDepartment(DepartmentId);
}
返回 JSON:
{"d":[{"__type":"Application.Controllers.PresentationObjects.EmployeePO","Positions":[{"__type":"Application.Controllers.PresentationObjects.PositionPO","Id":4,"Name":"Employee: 1test Position 1","IsPriority":false,"WarningThreshold":50,"CriticalThreshold":60,"CurrentWaitingTime":-1,"Passengers":[],"Qualifications":[...
I'm using a .net script service which is called from client-side script, and it works very nicely.
only problem is- it generates a '__type' attribute for each of the returned objects, which I don't want or need.
I've seen a few posts about this over the web, and as far as I could tell, there are only 'workarounds' for this:
some people suggested hiding the parameter-less c'tor of the return type as 'internal protected',
others suggested not using the [ScriptMethod] tag, and instead JSONfy the result manually and return a string.
I'm wondering whether there is another, better, solution for this. and by the way- what is this attribute used for, anyway?
I'm enclosing the service method and the generated JSON.
method:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
public IEnumerable<EmployeePO> GetEmployeesForDepartment(int DepartmentId)
{
return new AdministrationController().GetEmployeesForDepartment(DepartmentId);
}
JSON returned:
{"d":[{"__type":"Application.Controllers.PresentationObjects.EmployeePO","Positions":[{"__type":"Application.Controllers.PresentationObjects.PositionPO","Id":4,"Name":"Employee: 1test Position 1","IsPriority":false,"WarningThreshold":50,"CriticalThreshold":60,"CurrentWaitingTime":-1,"Passengers":[],"Qualifications":[...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好的,所以我最终采纳了 Jhon Morrison 的建议,该建议发布在问题@Kid 链接到-
我将返回的类上的所有无参数构造函数定义为受保护的内部函数,这确实达到了目的。
在我实际上需要创建空对象的情况下,我创建了一个 Factory 方法,如下所示:
@Kid 的答案也有效,但这样对我来说看起来更干净。
OK so I ended up taking the advice from Jhon Morrison, posted in the question @Kid linked to-
I defined all the parameter-less constructors on my returned classes as protected internal and that really did the trick.
In cases where I actually needed to create empty objects I created a Factory method, like so:
@Kid's answer also worked, but it looked cleaner to me this way.
我注意到作为对象返回的匿名类型根本不产生 __Type 属性。我出于不同的原因返回了一些像这样的对象,Javascript 不在乎,它喜欢匿名类型。所以我想我将开始转换许多其他类型,将它们转换为所有对象继承的基础对象。服务层将处理这个问题,因为我喜欢传递强类型,并且直到最后一刻才想转换它。这个选角需要时间吗?大概。值得吗?哎呀,我不知道。我同意,出于安全原因,这些信息不应该在那里。来吧微软。
I have noticed that anonymous types returned as object do not produce the __Type attribute at all. I had returned some of my objects like this for different reasons and Javascript dont care, it loves anonymous types. So I suppose I will start converting many of the other types casting them down to the base object that all objects inherit from. The service layer will handle this because I like passing around strong types and don't want to convert it until the last minute. Does this casting take time? Probably. Is it worth it? Geez, I don't know. I agree tho, that this info should not be in there for security reasons. C'mon Microsoft.
我做得有点不同,对我来说更干净一些。 (我并不是想放弃之前的答案,只是想添加/减少所需的编码量。)
I did it a little different and for me a little cleaner. (I'm not trying to take away from the previous answer, just trying to add / reduce the amount of coding required.)
如果将返回类型从
IEnumerable
更改为IEnumerable
If you change the return type from
IEnumerable<EmployeePO>
toIEnumerable<Object>
, the__type
field won't be added to the JSON string.