【原创】如何让SharePoint2010的内联代码支持.Net framework 3.5[How to embed inline code in aspx with .net framework 3.5 syntax]

SharePoint 2010是建立在.Net framework 3.5基础上的,所以其必然要支持3.5的语法。在SharePoint开发过程中,对于系统的实现可以有多种方法,其中之一是不被微软所提倡的内联代码。所谓内联代码就是直接在aspx中编写服务器端代码,其介绍文章在网上有很多,例如:http://blog.pixelmill.com/1037/sharepoint-2010-allow-server-side-code-inline-code-blocks/http://www.hexanes.com/?p=408等等。但是,这种嵌入的代码只支持2.0的语法,所以像Linq、var等都不能支持,想要支持的话,需要修改网站的web.config。

1、修改compilation节中的配置,例如我将其修改为:

    <compilation batch="false" debug="true" optimizeCompilations="true">
      <assemblies>
        <add assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
        <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        <add assembly="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
        <add assembly="Microsoft.SharePoint.Search, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
        <add assembly="Microsoft.Office.Server.Search, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
        <add assembly="Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        <add assembly="Microsoft.SharePoint.IdentityModel, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
        <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>        
      </assemblies>
.......
</compilation>

 

这里需要说明的是,如果在内联代码中,还要调用其它的类库(例如自己写的或者第3方的),也可以通过这种方式加入到这里(好像放在Bin目录也是可以的,但各有利弊吧)。

 

2、加入system.codedom节配置:在configuration节中加入以下配置

  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4"
                type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0,  
                    Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <providerOption name="CompilerVersion" value="v3.5" />
        <providerOption name="WarnAsError" value="false" />
      </compiler>
    </compilers>
  </system.codedom>  

 

这样就可以在内联代码中编写.net framework 3.5的代码了,如在我的页面中:

<%@ Page Language="C#" masterpagefile="~masterurl/default.master" title="areaTrend" inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" meta:progid="SharePoint.WebPartPage.Document" meta:webpartpageexpansion="full" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="xxxx.Web.Security" %>
<%@ Import Namespace="xxxx.Web.Security.Model" %>
<%@ Import Namespace="xxxx.SPLibraryExtend.Utils" %>
<%@ Import Namespace="xxxx.SPLibraryExtend" %>

<script type="text/c#" runat="server">        
    void Page_Load(object sender, System.EventArgs e) 
    {
        
    } 
</script>
<asp:Content ID="ContentTitle" runat="server" ContentPlaceHolderID="PlaceHolderPageTitle">
    xxxxxxxxx</asp:Content>
<asp:Content ID="ContentPageHead" runat="server" ContentPlaceHolderID="PlaceHolderAdditionalPageHead">
    <link rel="stylesheet" type="text/css" href="/Style Library/common/style/xxxx.css" />
    <style type="text/css">
        .infoList_dept li { width:270px; float:left; margin-left:25px; line-height:28px;}
    </style>
</asp:Content>
<asp:Content id="Content1" runat="server" contentplaceholderid="PlaceHolderMain">
<div class="grid_12">
<div class="border_gray" >
    <div class="title_orange">
        <div class="dot_orange"></div>
        <div class="title_text"></div>
    </div>
    <div class="padding_20">
    <!--内容开始-->
 
        <%
            SqlRoleExProvider role=null;
            foreach (var provider in Roles.Providers)
            {
                role = provider as SqlRoleExProvider;
                if (role!=null)
                {
                    break;
                }
            }
                    
            List<RoleExModel> lstParent = new List<RoleExModel>();
            
            lstParent.Add(role.GetRoleExModelByName("分类1"));
            lstParent.Add(role.GetRoleExModelByName("分类2"));
            lstParent.Add(role.GetRoleExModelByName("分类3"));
            foreach (var p in lstParent)
            {
        %><div class="title_white_orange">
          <div class="dot_orange"></div>
                    <div class="title_text"><%=p.RoleName %></div>
                </div>
                <div class="empty_5"></div>
                <ul class="infoList_dept">
        <%
                List<RoleExModel> lstChild = (List<RoleExModel>)role.getRoleExModelsByParentId(p.RoleId).OrderBy(t => t.Sequence).ToList();                
                foreach (var c in lstChild)
                {
        %><li><a href="/xxxx.aspx?DeptGuid=<%=c.RoleId %>&DeptId=<%=FBARoleHelper.GetFBARoleId(c.RoleName) %>" target="_blank"><%=c.RoleName %></a></li>
        <%
                }
        %></ul>
                <div class="clear"></div>
                <div class="empty_20"></div>
        <%                    
            }
        %><!--内容结束--></div>
</div></div>
</asp:Content>
posted @ 2012-09-06 22:21  吴东雷  阅读(468)  评论(0编辑  收藏  举报