不同项目之间的控件共享

不同项目之间的控件共享 

      上篇blog我有提到,不知如何解决不用项目之间的控件共享问题.很多朋友给予了热心的回答,这里一起表示感谢.
       
总结大家的回复,一般有2种观点,一是做成用户控件,二是js+css的形式.确实现在这2种方法很多见,各有优点,但也各有缺点.做成用户控件的话,如果每次修改都要发布dll的话,无疑是非常麻烦的,js+css也是一种非常好的方法,应该很多网站目前都是使用这个方法来共享头部和底部的.js的方法很便捷,但是也缺少一定的灵活性.因为我们不仅仅是共享头尾而已.

      比如文章的列表,不同板块的文章会呈现不用的文章和表现形式.他们只是调用的内容和排版上有所不同,而代码方面应该是一致的.这个时候就需要做成用户控件了.那么该如何解决这个问题呢?

      我们先设想一下大概思路,不同项目之间共享控件,那么控件就应该做成一个独立的项目,而同一个控件的代码是一样的(就是ascx.cs是一样的),变现不一样(就是ascx不一样).

肯定有朋友会想到了,我们建立多个ascx文件,让他们都继承自同一个ascx.cs不久可以了吗?

 

       

,没错,确实是这样的思路,但是问题在于,.NET的控件只能被同一个项目所引用,因为他只支持虚拟的相对路径.一旦跨项目引用了就出提示错误.(超出了当先项目虚拟目录的根目录了)

         经过我们Google+baidu,发现.net 2.0提供了一些类来实现自己的虚拟文件系统. 这些类在System.Web.Hosting命名空间内定义.我们只要重写VirtualPathProviderVirtualFile就可以把ascx放在一个统一的地方,然后各个项目都可以调用了.

       具体代码如下:

          

ContractedBlock.gif ExpandedBlockStart.gif Code
  1public class SkinVirtualPathProvider : VirtualPathProvider
  2
  3ExpandedBlockStart.gifContractedBlock.gif    {
  4
  5ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
  6
  7        /// 定义模板路径
  8
  9        /// </summary>

 10
 11        public string VirtualPath
 12
 13ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 14
 15            get;
 16
 17            set;
 18
 19        }

 20
 21 
 22
 23ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 24
 25        /// 定义扩展名
 26
 27        /// </summary>

 28
 29        public string ExtensionString
 30
 31ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 32
 33            get;
 34
 35            set;
 36
 37        }

 38
 39 
 40
 41        private bool CheckPathInExtension(string virtualPath)
 42
 43ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 44
 45            return virtualPath.EndsWith(ExtensionString);
 46
 47        }

 48
 49 
 50
 51        public override bool FileExists(string virtualPath)
 52
 53ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 54
 55            return (CheckPathInExtension(virtualPath) || Previous.FileExists(virtualPath));
 56
 57        }

 58
 59 
 60
 61        public override VirtualFile GetFile(string virtualPath)
 62
 63ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 64
 65            if (!CheckPathInExtension(virtualPath))
 66
 67                return base.GetFile(virtualPath);
 68
 69            else
 70
 71ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 72
 73                return new SkinVirtualFile(this,virtualPath);
 74
 75            }

 76
 77        }

 78
 79 
 80
 81        public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
 82
 83ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 84
 85            if (CheckPathInExtension(virtualPath))
 86
 87                return new CacheDependency(this.VirtualPath + "/" + virtualPath);
 88
 89 
 90
 91            else
 92
 93                return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
 94
 95            
 96
 97        }

 98
 99    }

100
101 
102
103 
104
105public class SkinVirtualFile:VirtualFile
106
107ExpandedBlockStart.gifContractedBlock.gif    {
108
109        string path;
110
111        private SkinVirtualPathProvider provider;
112
113 
114
115        public SkinVirtualFile(SkinVirtualPathProvider provider, string virtualPath)
116
117            : base(virtualPath)
118
119ExpandedSubBlockStart.gifContractedSubBlock.gif        {
120
121            path = provider.VirtualPath + "/" + virtualPath;
122
123            this.provider = provider;
124
125        }

126
127 
128
129        public override Stream Open()
130
131ExpandedSubBlockStart.gifContractedSubBlock.gif        {
132
133            return new FileStream(this.path, FileMode.Open, FileAccess.Read, FileShare.Read);
134
135        }

136
137}

138
139 
140

然后在Global里注册一下:

ContractedBlock.gif ExpandedBlockStart.gif Code
  1public class SkinVirtualPathProvider : VirtualPathProvider
  2
  3ExpandedBlockStart.gifContractedBlock.gif    {
  4
  5ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
  6
  7        /// 定义模板路径
  8
  9        /// </summary>
 10
 11        public string VirtualPath
 12
 13ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 14
 15            get;
 16
 17            set;
 18
 19        }
 20
 21 
 22
 23ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 24
 25        /// 定义扩展名
 26
 27        /// </summary>
 28
 29        public string ExtensionString
 30
 31ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 32
 33            get;
 34
 35            set;
 36
 37        }
 38
 39 
 40
 41        private bool CheckPathInExtension(string virtualPath)
 42
 43ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 44
 45            return virtualPath.EndsWith(ExtensionString);
 46
 47        }
 48
 49 
 50
 51        public override bool FileExists(string virtualPath)
 52
 53ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 54
 55            return (CheckPathInExtension(virtualPath) || Previous.FileExists(virtualPath));
 56
 57        }
 58
 59 
 60
 61        public override VirtualFile GetFile(string virtualPath)
 62
 63ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 64
 65            if (!CheckPathInExtension(virtualPath))
 66
 67                return base.GetFile(virtualPath);
 68
 69            else
 70
 71ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 72
 73                return new SkinVirtualFile(this,virtualPath);
 74
 75            }
 76
 77        }
 78
 79 
 80
 81        public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
 82
 83ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 84
 85            if (CheckPathInExtension(virtualPath))
 86
 87                return new CacheDependency(this.VirtualPath + "/" + virtualPath);
 88
 89 
 90
 91            else
 92
 93                return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
 94
 95            
 96
 97        }
 98
 99    }
100
101 
102
103 
104
105public class SkinVirtualFile:VirtualFile
106
107ExpandedBlockStart.gifContractedBlock.gif    {
108
109        string path;
110
111        private SkinVirtualPathProvider provider;
112
113 
114
115        public SkinVirtualFile(SkinVirtualPathProvider provider, string virtualPath)
116
117            : base(virtualPath)
118
119ExpandedSubBlockStart.gifContractedSubBlock.gif        {
120
121            path = provider.VirtualPath + "/" + virtualPath;
122
123            this.provider = provider;
124
125        }
126
127 
128
129        public override Stream Open()
130
131ExpandedSubBlockStart.gifContractedSubBlock.gif        {
132
133            return new FileStream(this.path, FileMode.Open, FileAccess.Read, FileShare.Read);
134
135        }
136
137}
138
139 
140

这里扩展名我定义的.skin,这里我们也web.config里也要注册一下

<buildProviders>

        <add extension=".skin" type="System.Web.Compilation.UserControlBuildProvider"/>

      </buildProviders>

 

如此这般操作了以后,比如项目中要使用1.skin 就会去 skin目录下去找这个文件了,不再受到虚拟目录的根目录的限制了.

解决这个问题之后,我们就要修改我们的用户控件了,让他会load这个skin里面的内容,然后来做数据的绑定.

具体代码我就不贴了,提供几个参考链接.

参考链接:

1.   http://www.cnblogs.com/cricket/articles/25299.html

2.       http://www.cnblogs.com/zxjay/archive/2008/11/13/xianfen_net_virtualpathprovider.html

3.       http://blog.csdn.net/baoaya/archive/2009/07/27/4384178.aspx

 

 

      没啥技术含量,主要是想分享一下解决问题的方法和思路,大家见笑了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值