2013-08-06 63 views

回答

1

而不是动作,如果您可以按照想要作为JSONP返回的对象类型的线思考,则可以像这样更改格式化程序,以仅允许某些类型被序列化为JSONP。

public override bool CanWriteType(Type type) 
{ 
    // Check type here and return true only for the types you want to allow JSONP 
    return true; 
} 

如果基于类型的过滤是不可能的,另一种选择将是不格式化添加到格式化的收集和明确指定JsonpFormatter,这样,只有在要返回JSONP的操作方法。

return new HttpResponseMessage() 
{ 
    Content = new ObjectContent<MyType>(anInstanceOfMyType, new JsonpFormatter()) 
}; 

但是,一个缺点是只有JSONP不管接口是什么,都会返回。