2013-08-06 67 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不管接口是什麼,都會返回。