.NET脚本的Web服务:__type属性脚本、属性、NET、__type

2023-09-04 04:45:38 作者:西风乍起

我使用的是从客户端脚本调用一个.NET脚本服务,它工作得很好。

唯一的问题是 - 它会生成一个__type'属性为每个返回的对象,我不想要或需要的。照片 我已经看到了这个几个帖子在网上,而据我所知,只有变通办法对这样的:

有人建议躲在返回类型的无参数的c'tor为内部保护,

其他建议不要使用手动[ScriptMethod]标签,而是JSONfy的结果,并返回一个字符串。

我不知道是否有此另一个更好的解决方案。并由方式 - 什么是用于该属性啊? 我在封闭的服务方法和产生的JSON。

方法:

  [WebMethod的]
[ScriptMethod(ResponseFormat = ResponseFormat.Json,UseHttpGet =真)
公开的IEnumerable< EmployeePO> GetEmployeesForDepartment(INT DepartmentID的)
{

        返回新AdministrationController()GetEmployeesForDepartment(DepartmentID的)。

}
 

JSON返回:

{"d":[{"__type":"Application.Controllers.$p$psentationObjects.EmployeePO","Positions":[{"__type":"Application.Controllers.$p$psentationObjects.PositionPO","Id":4,"Name":"Employee: 1test位置1","IsPriority":false,"WarningThreshold":50,"CriticalThreshold":60,"CurrentWaitingTime":-1,"Passengers":[],"Qualifications":[...

解决方案

行,所以我最终采取的意见为Jhon·莫里森,张贴在这个问题@Kid链接与 我定义的所有对我的恢复类无参数的构造函数是受保护的内部,并且确实的伎俩。 在这里我真的需要创建空的对象的情况下我创建了一个工厂方法,像这样:

 公共类MyClass的
    {
        公众诠释编号{获得;组; }
        /*...*/

        公共MyClass的(INT ID):
        {
            ID = ID;
        }


        公共静态MyClass的CreateNewZonePO()
        {
            返回新MyClass的();
        }

        //参数的c'tor进行序列化的缘故
        //它是受保护的内部,以避免__type属性从Web服务返回的时候。看到http://stackoverflow.com/questions/4958443/net-script-web-services-type-attribute
        受保护的内部MyClass的()
        {

        }
    }
 
ultidev web服务器下载 小型.net web服务器下载 当易网

@孩子的回答也工作,但它看起来更清洁,以我这样的。

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":[...

解决方案

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.