基于 HtmlHelper 的自定义扩展Container
2017-08-07 09:59
435 查看
基于 HtmlHelper 的自定义扩展Container
Intro
基于 asp.net mvc 的权限控制系统的一部分,适用于对UI层数据呈现的控制,基于HtmlHelper的扩展组件
Code
基于 asp.net mvc 的权限控制系统示例代码:https://github.com/WeihanLi/AccessControlDemo权限控制核心代码:https://github.com/WeihanLi/AccessControlDemo/tree/master/AccessControlHelper
SparkContainer 代码:
public class SparkContainer : IDisposable { private readonly string _tagName; private readonly ViewContext _viewContext; private readonly bool _canAccess; private bool _disposed; private readonly string _content; public SparkContainer(ViewContext viewContext, string tagName, bool canAccess = true) { _viewContext = viewContext; _tagName = tagName; _canAccess = canAccess; if (!_canAccess) { _content = (_viewContext.Writer as StringWriter).GetStringBuilder().ToString(); } } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!_disposed) { _disposed = true; EndShopContainer(); } } public void EndShopContainer() { if (!_canAccess) { (_viewContext.Writer as StringWriter).GetStringBuilder().Clear().Append(_content); } else { _viewContext.Writer.Write("</{0}>", _tagName); } } }
扩展方法
/// <summary> /// SparkContainer /// </summary> /// <param name="helper">HtmlHelper</param> /// <param name="tagName">标签名称</param> /// <param name="attributes">htmlAttributes</param> /// <param name="accessKey">accessKey</param> /// <returns></returns> public static SparkContainer SparkContainer(this HtmlHelper helper, string tagName, object attributes = null, string accessKey = "") { // ... return SparkContainerHelper(helper, tagName, HtmlHelper.AnonymousObjectToHtmlAttributes(attributes), displayStrategy.IsControlCanAccess(accessKey)); } private static SparkContainer SparkContainerHelper(this HtmlHelper helper, string tagName, IDictionary<string, object> attributes = null, bool canAccess = true) { // ... TagBuilder tagBuilder = new TagBuilder(tagName); if (canAccess) { tagBuilder.MergeAttributes(attributes); helper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag)); } return new SparkContainer(helper.ViewContext, tagName, canAccess); }
Use
使用说明:@using(Html.SparkContainer("div",new { @class="container",custom-attribute = "abcd" })) { @Html.Raw("1234") }
没有权限访问时就不会将内容渲染到页面,有权限访问时实际渲染生成的 Html 如下:
<div class="container" custom-attribute="abcd"> 1234 </div>
Contact
如果您有什么问题或建议,欢迎与我联系 weihanli@outlook.com相关文章推荐
- Asp.net MVC Preview 4 中自定义Jquery的HtmlHelper扩展
- mvc基础系列说谈(4)——HtmlHelper,自定义,扩展HtmlHelper
- Asp.Net MVC对类HtmlHelper的自定义扩展方法以及如何调用
- Asp.net MVC Preview 4 中自定义Jquery的HtmlHelper扩展
- 自定义HtmlHelper扩展方法
- MVC自定义HtmlHelper扩展方法.
- ASP.NET MVC HtmlHelper 自定义扩展分页功能
- Asp.net MVC Preview 4 中自定义Jquery的HtmlHelper扩展
- dhl: 在 ASP.NET MVC 中创建自定义 HtmlHelper 使用扩展方法创建 HTML Helper
- ASP.Net MVC开发基础学习笔记(2):HtmlHelper与扩展方法
- 转:ASP.NET MVC扩展之HtmlHelper辅助方法
- 七、创建自定义的HTML Helper 转
- Dubbo源码分析(三):自定义Schema--基于Spring可扩展Schema提供自定义配置支持(spring配置文件中 配置标签支持)
- ASP.Net MVC开发基础学习笔记(2):HtmlHelper与扩展方法
- [转]ASP.NET MVC HtmlHelper扩展之Calendar日期时间选择
- iOS App开发中扩展RCLabel组件进行基于HTML的文本布局
- .NET/ASP.NETMVC 深入剖析 Model元数据、HtmlHelper、自定义模板、模板的装饰者模式2
- 通过一个MVC HtmlHelper扩展实例,简单说明扩展步骤
- MVC学习系列——HtmlHelper扩展
- [ASP.NET MVC]为HtmlHelper添加一个RadioButtonList扩展方法