1

我正在尝试在 SharePoint 2010 的 aspx 页面中使用 C# 代码。我不断收到“不允许代码块”错误。

我的 aspx 页面位于(服务器)/SitePages/ajax.aspx:(如果重要,在 SharePoint Designer 2010 中编辑)

<%@ Page Language="C#" %>
<script runat="server">
    Response.Write("Hello world");
</script>

我在 C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG 的 web.config 中添加了以下内容

<PageParserPath VirtualPath="/SitePages/ajax.aspx" CompilationMode="Always" AllowServerSideScript="true" />

这是我添加到 web.config 中的同一行,在上下文中显示:

<SharePoint>
  <SafeMode 
        MaxControls = "200"  
        CallStack = "false"
        DirectFileDependencies ="10"
        TotalFileDependencies = "50"
        AllowPageLevelTrace = "false"
        >
        <PageParserPaths>
            <PageParserPath VirtualPath="/SitePages/ajax.aspx" CompilationMode="Always" AllowServerSideScript="true" />
        </PageParserPaths>

为什么我仍然收到“代码块”错误?某处是否有另一个安全开关?

(我知道自定义 Web 部件是首选解决方案,但我认为这还不够,因为返回值应该是 json - 这是一个通过 ajax 访问以获取数据的页面。)

4

2 回答 2

0

When you upload an aspx page to a document library (which is what you're doing when you use SharePoint Designer) that aspx page can't have inline code blocks and it can't have a code behind. This is primarily a security mechanism. It prevents any old user from uploading an aspx page with malicious content that will then be executed with full privileges on your server, and also serving content to (potentially) any user.

For an aspx page to execute code it needs to be compiled into a WSP and deployed to the GAC on the server. When you do that you can either use inline code blocks or, better yet, have an aspx page with a code behind. To publish that page to the site you would need to compile the project in visual studio into a WSP, deploy that to the server by logging into the machine with sufficient privileges, and then add and deploy the code. This ensures that non-developers can't upload executable code to your site.

Finally, on a more unrelated note, since you don't actually want to display a page, but just JSON content, you probably shouldn't use an aspx page at all (although you can). You should probably just create an HTTP handler or a web service that writes the appropriate content out.

于 2012-11-14T18:13:38.610 回答
0

I suppose, you have to define PageParserPath element in an Web application web.config that is located C:\inetpub\wwwroot\wss\VirtualDirectories[application folder]\web.config.

于 2012-11-14T18:21:59.780 回答