.net 脚本 Web 服务:__type 属性

发布于 2024-10-16 23:23:01 字数 989 浏览 3 评论 0原文

我正在使用从客户端脚本调用的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瀟灑尐姊 2024-10-23 23:23:01

好的,所以我最终采纳了 Jhon Morrison 的建议,该建议发布在问题@Kid 链接到-
我将返回的类上的所有无参数构造函数定义为受保护的内部函数,这确实达到了目的。
在我实际上需要创建空对象的情况下,我创建了一个 Factory 方法,如下所示:

public class MyClass
    {
        public int Id { get; set; }
        /*...*/

        public MyClass(int id):
        {
            Id = id;
        }


        public static MyClass CreateNewZonePO()
        {
            return new MyClass();
        }

        //parameterless c'tor for serialization's sake
        //it's protected internal in order to avoid the "__type" attribute when returned from a web service. see http://stackoverflow.com/questions/4958443/net-script-web-services-type-attribute
        protected internal MyClass()
        {

        }
    }

@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:

public class MyClass
    {
        public int Id { get; set; }
        /*...*/

        public MyClass(int id):
        {
            Id = id;
        }


        public static MyClass CreateNewZonePO()
        {
            return new MyClass();
        }

        //parameterless c'tor for serialization's sake
        //it's protected internal in order to avoid the "__type" attribute when returned from a web service. see http://stackoverflow.com/questions/4958443/net-script-web-services-type-attribute
        protected internal MyClass()
        {

        }
    }

@Kid's answer also worked, but it looked cleaner to me this way.

一身软味 2024-10-23 23:23:01

我注意到作为对象返回的匿名类型根本不产生 __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.

迟月 2024-10-23 23:23:01

我做得有点不同,对我来说更干净一些。 (我并不是想放弃之前的答案,只是想添加/减少所需的编码量。)

public class MyClass
{ 
    private MyClass(int id) { } //needs to have a constructor with at least one parameter passed to it. Update: looks like this is not needed. I still need to test this some more though.

    protected internal MyClass() { } //this actually drives the restriction on the JSON return of the __type attribute and is an overload constructor
}

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.)

public class MyClass
{ 
    private MyClass(int id) { } //needs to have a constructor with at least one parameter passed to it. Update: looks like this is not needed. I still need to test this some more though.

    protected internal MyClass() { } //this actually drives the restriction on the JSON return of the __type attribute and is an overload constructor
}
亢潮 2024-10-23 23:23:01

如果将返回类型从 IEnumerable 更改为 IEnumerable__type 字段将不会添加到 JSON 字符串。

If you change the return type from IEnumerable<EmployeePO> to IEnumerable<Object>, the __type field won't be added to the JSON string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文