文章出處

基于 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 代碼:

 1 public class SparkContainer : IDisposable
 2     {
 3         private readonly string _tagName;
 4         private readonly ViewContext _viewContext;
 5         private readonly bool _canAccess;
 6         private bool _disposed;
 7 
 8         private readonly string _content;
 9 
10         public SparkContainer(ViewContext viewContext, string tagName, bool canAccess = true)
11         {
12             _viewContext = viewContext;
13             _tagName = tagName;
14             _canAccess = canAccess;
15             if (!_canAccess)
16             {
17                 _content = (_viewContext.Writer as StringWriter).GetStringBuilder().ToString();
18             }
19         }
20 
21         public void Dispose()
22         {
23             Dispose(true);
24             GC.SuppressFinalize(this);
25         }
26 
27         protected virtual void Dispose(bool disposing)
28         {
29             if (!_disposed)
30             {
31                 _disposed = true;
32                 EndShopContainer();
33             }
34         }
35 
36         public void EndShopContainer()
37         {
38             if (!_canAccess)
39             {
40                 (_viewContext.Writer as StringWriter).GetStringBuilder().Clear().Append(_content);
41             }
42             else
43             {
44                 _viewContext.Writer.Write("</{0}>", _tagName);
45             }
46         }
47     }

 

擴展方法

 /// <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 如下:

1 <div class="container" custom-attribute="abcd">
2     1234
3 </div>

 

Contact

如果您有什么問題或建議,歡迎與我聯系 weihanli@outlook.com


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()