今天,要给各位演示一个Asp.Net3.5 下面的Linq的Hello World.
算是最简化的Hello吧。
首先,Web.Config最最简化的配置如下:
解释在代码后面。


<configuration>
  <system.web>
          <compilation debug="false">
      <assemblies>
        <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                  </assemblies>
    </compilation>
  </system.web>
  <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"/>
      </compiler>
    </compilers>
  </system.codedom>
</configuration>


下面解释一下。System.Core.dll里面包含了system.linq所以必须加入这个 assembly,
当然这个assembly也可以不在web.config里面加入,也就是说这个systen.web可以去掉,但是你需要在aspx文件里面用古老的 <%@ Register TagPrefix="sc" Namespace="System.Linq" Assembly= "System.Core" %>来加入,并且还需要在bin里面加入system.Core.dll这个文件,麻烦。
第二个重点是告诉他用v3.5的编译器。因为asp.net3.5实际上用的是asp.net2.0的瓤,安装3.5了之后iis里面还是显示2.0所以在web.config里面要说明一下,用3.5,要不然他不认linq的什么var xxx=from云云。
下面是aspx主体:

<%@ Import Namespace="System.Linq" %>
<script language="C#" runat="server">
          protected  void  Page_Load(object  sender,  EventArgs  e) 
          { 
    txtValidated.Text="Sipo(www.51aspx.com),最后输出:";
    int[] testnums = { 787,321,4,22,1,-83 };
    var nums =from s in testnums where s < 5 select s;
    foreach (var c in nums) txtValidated.Text+=c+",";
          }
</script>
<form runat="server">
<asp:TextBox />
</form>


没有用codebehind是因为节省地方。helloworld嘛~一个文件就搞定。
AutoEventWireup="True"是要执行Page_Load事件,要不然不执行。
在这里要import System.Linq,cs文件里面用using.
然后要说的就没什么了吧(作者:Sipo)。

相关文章:

  • 2022-02-15
  • 2021-07-25
  • 2022-12-23
  • 2022-12-23
  • 2021-11-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-08-26
  • 2021-08-10
  • 2022-12-23
  • 2022-12-23
  • 2021-11-20
  • 2022-12-23
相关资源
相似解决方案