惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
GbyAI
GbyAI
SecWiki News
SecWiki News
Project Zero
Project Zero
C
Cisco Blogs
Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Scott Helme
Scott Helme
A
Arctic Wolf
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
Apple Machine Learning Research
Apple Machine Learning Research
T
Tailwind CSS Blog
The Hacker News
The Hacker News
T
Tenable Blog
雷峰网
雷峰网
有赞技术团队
有赞技术团队
V
V2EX
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threat Research - Cisco Blogs
T
Threatpost
AWS News Blog
AWS News Blog
L
LINUX DO - 热门话题
Application and Cybersecurity Blog
Application and Cybersecurity Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
SegmentFault 最新的问题
月光博客
月光博客
Spread Privacy
Spread Privacy
S
Secure Thoughts
宝玉的分享
宝玉的分享
博客园 - 三生石上(FineUI控件)
Forbes - Security
Forbes - Security
T
The Exploit Database - CXSecurity.com
G
GRAHAM CLULEY
The Last Watchdog
The Last Watchdog
Y
Y Combinator Blog
I
Intezer
博客园 - 【当耐特】
B
Blog RSS Feed
Attack and Defense Labs
Attack and Defense Labs
I
InfoQ
博客园 - 叶小钗
Cyberwarzone
Cyberwarzone
V2EX - 技术
V2EX - 技术
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hugging Face - Blog
Hugging Face - Blog
H
Help Net Security
C
CERT Recently Published Vulnerability Notes

博客园 - SAL

【转】WinForm窗体显示和窗体间传值 【转】Emgu CV on C# (五) —— Emgu CV on 局部自适应阈值二值化 常用的几种OCR方法/组件小结(C#) URL重写html后Html文件打不开解决办法 【转】SQL SERVER 2005/2008 中关于架构的理解 Visual Studio、.net framework、CLR与JDK、JRE、JVM、Eclipse 【转】让Entity Framework不再私闯sys.databases 【转】MVC Model建模及Entity Framework Power Tool使用 【转】NuGet学习笔记 【转】一点一点学ASP.NET之基础概念——HttpModule 【转】C#微信公众平台开发者模式开启代码 【转】合理的布局,绚丽的样式,谈谈Winform程序的界面设计 【转】winform程序textbox滚动条保持在最下面 内容不闪烁 【转】检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为【经典】模式)。 【转】C#中的委托,匿名方法和Lambda表达式 【转】HttpWebRequest 保持session 【转】WCF 服务第一次调用慢的问题 【转】“无法从http://XXX/XXX.svc?wsdl获取元数据”错误的解决方法 【转】开源Word读写组件DocX介绍与入门
【转】如何在ASP.NET 2.0中定制Expression Builders
SAL · 2013-09-27 · via 博客园 - SAL

expressions是asp.net 2.0中的新特色,它可以使你在asp.net的页面里很方便的使用自定义的属性. 在ASPX页里只要使用$符号就可以访问到,你定制的属性了. 例如我们看个例子: ASPX页面中如下:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$connectionStrings:Pubs %>" SelectCommand="select * from catalog"></asp:SqlDataSource>

web.config文件中如下:

<configuration> 
    <appSettings/> 
  <connectionStrings> 
    <add name="Pubs" connectionString="server=localhost;database=getwant;Trusted_Connection=yes"/> 
  </connectionStrings> 
</configuration> 

因为在web.config中默认就有了connectionStrings的这个节点,所以我们很方便的使用add增加了一个属性Pubs. 而如何自定义我们自己使用的节点呢?例如:<%$ Version:MajorMinor%>可以显示当前环境下asp.net的主版本号和次版本号呢? 如果我们直接在页面中输入上面的表达式,编译器会告诉你,Version并没有被定义,请在expressionBuilders节点中定制.其实这时候就要用到ExpressionBuilder类了.
System.Web.Compilation.ExpressionBuilder 就是expression builders的基类. 我们看看web.config中的设置:

<compilation debug="true">
            <expressionBuilders>
                <add expressionPrefix="Version" type="VersionExpressionBuilder"/>
            </expressionBuilders>
        </compilation>

怎么样是不是很简单呢?定义一个expressionPrefix为Version就可以了. 不过有人说那个type后面的是什么意思呢,有VersionExpressionBuilder这个类吗? 其实这个是我们自己继承了ExpressionBuilder的类.

public class VersionExpressionBuilder:ExpressionBuilder
{
    public override CodeExpression GetCodeExpression(BoundPropertyEntry entry,object parsedData,ExpressionBuilderContext context)
    {
        string param = entry.Expression;
        if (string.Compare(param, "All", true) == 0)
        {
            return new CodePrimitiveExpression(string.Format("{0}.{1},{2}.{3}", Environment.Version.Major, Environment.Version.Minor, +
                Environment.Version.Build, Environment.Version.Revision));
        }
        else if (string.Compare(param, "MajorMinor", true) == 0)
        {
            return new CodePrimitiveExpression(string.Format("{0}.{1}", Environment.Version.Major, Environment.Version.Minor));
        }
        else
            throw new InvalidOperationException("User $ Version:All or $ Version:MajorMinor");
    }
}

这时候我们在ASPX页面中如下设置就可以通过编译了:

 ASP.NET  <asp:Literal ID="Literal1" runat="server" Text="<% $ Version:MajorMinor %>"></asp:Literal>

显示的为"ASP.NET 2.0" 把表示式改为:<%$ Version:All %>就会显示为"ASP.NET 2.0,50727.42 "