在WebParts的RenderControl()中使用WebRequest.Create

时间:2010-09-20 15:50:01

标签: .net vb.net sharepoint httpwebrequest web-parts

我需要调用一个Web服务来接收一个JSON对象,我将在呈现内容之前对其进行解析。这是相同的代码:

Dim jResponse As MessageResponseWrapper = New MessageResponseWrapper() 'Custom class to deserialize JSON
        Dim req As System.Net.HttpWebRequest
        req = System.Net.WebRequest.Create("http://some-cross-domain?q=" & SearchKeyword & "&restapi.response_style=view&xslt=json.xsl")
        req.Method = WebRequestMethods.Http.Get
        Dim res As HttpWebResponse = req.GetResponse()
        Dim reader As New StreamReader(res.GetResponseStream())
        Dim json As String = reader.ReadToEnd()

        Dim ser As New DataContractJsonSerializer(GetType(MessageResponseWrapper))
        Using ms As New MemoryStream(Encoding.Unicode.GetBytes(json))
            jResponse = TryCast(ser.ReadObject(ms), MessageResponseWrapper)
        End Using

这是一个独立的ASP.Net页面。但是,我需要将其作为Web部件移植到Sharepoint站点。使用描述的方法here我在RenderControl()方法中给出了上面的代码。我收到错误“The”CustomWebPart“Web部件似乎导致问题。请求获得类型'System.Net.WebPermission,System,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'的权限失败。”

我尝试在web.config中包含以下行的变体,但没有用。

<SafeControl Assembly="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Namespace="System.Net" TypeName="WebPermission" Safe="True" AllowRemoteDesigner="True" />

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

将Webpart部署到GAC工作。 我之前手动部署它 - 将dll复制到bin文件夹

部署到GAC会授予进行WebRequest.Create()

等调用的权限

Sharepoint安全性参考:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_sp2003_ta/html/sharepoint_wsscodeaccesssecurity.asp