简体   繁体   English

在WebParts的RenderControl()内部使用WebRequest.Create

[英]Using WebRequest.Create inside RenderControl() for WebParts

I need to call a web service to receive a JSON object which I'll parse before rendering the content. 我需要调用一个Web服务来接收一个JSON对象,该对象将在呈现内容之前进行解析。 Here's the code for the same: 这是相同的代码:

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

This works as a stand alone ASP.Net page. 这可以作为独立的ASP.Net页面使用。 However, I need this as a Web Part to port to a Sharepoint site. 但是,我需要将此作为Web部件移植到Sharepoint站点。 Using the method described here I've given the above code in the RenderControl() method. 使用这里描述的方法我已经在RenderControl()方法中给出了上面的代码。 I get the error "The "CustomWebPart" Web Part appears to be causing a problem. Request for the permission of type 'System.Net.WebPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed." 我收到错误消息““ CustomWebPart” Web部件似乎引起问题。请求类型为'System.Net.WebPermission,System,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'的权限失败。”

I have tried including variations of the following line in web.config with no avail. 我尝试将以下行的变体包含在web.config中,但无济于事。

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

Any ideas? 有任何想法吗?

Deploying the webpart to GAC worked. 将Webpart部署到GAC的工作正常。 I was deploying it manually before - copy dll to bin folder 我之前是手动部署它-将dll复制到bin文件夹

Deploying to GAC give permissions to make calls like WebRequest.Create() 部署到GAC可以进行类似WebRequest.Create()的调用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM