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

推荐订阅源

The Cloudflare Blog
U
Unit 42
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
腾讯CDC
罗磊的独立博客
博客园 - 聂微东
博客园_首页
雷峰网
雷峰网
云风的 BLOG
云风的 BLOG
Jina AI
Jina AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
人人都是产品经理
人人都是产品经理
Y
Y Combinator Blog
量子位
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
小众软件
小众软件
月光博客
月光博客
T
The Exploit Database - CXSecurity.com
Google DeepMind News
Google DeepMind News
H
Help Net Security
O
OpenAI News
Blog — PlanetScale
Blog — PlanetScale
S
Security Affairs
S
Security @ Cisco Blogs
Microsoft Security Blog
Microsoft Security Blog
T
The Blog of Author Tim Ferriss
AI
AI
MongoDB | Blog
MongoDB | Blog
G
Google Developers Blog
MyScale Blog
MyScale Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
Docker
Hugging Face - Blog
Hugging Face - Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Schneier on Security
Cloudbric
Cloudbric
H
Heimdal Security Blog
J
Java Code Geeks
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
宝玉的分享
宝玉的分享
有赞技术团队
有赞技术团队
S
SegmentFault 最新的问题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
爱范儿
爱范儿
I
Intezer
GbyAI
GbyAI

博客园 - 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 "