基於 HtmlHelper 自訂擴充Container

來源:互聯網
上載者:User

標籤:int   hub   許可權   href   rate   basic   許可權控制   tree   div   

基於 HtmlHelper 自訂擴充ContainerIntro

基於 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             else43             {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     12343 </div>

 

Contact

如果您有什麼問題或建議,歡迎與我聯絡 [email protected]

基於 HtmlHelper 自訂擴充Container

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.