Custom extension of Container and HtmlHelper Based on htmlhelper

Source: Internet
Author: User

Custom extension of Container and HtmlHelper Based on htmlhelper
Htmlinerintro custom Extension Based on HtmlHelper

Part of the permission Control System Based on asp.net mvc, which is suitable for controlling the data presentation at the UI Layer.HtmlHelperExtension Components

Code

Example code of permission Control System Based on asp.net mvc: https://github.com/WeihanLi/AccessControlDemo

Permission control core code: https://github.com/WeihanLi/AccessControlDemo/tree/master/AccessControlHelper

SparkContainer code:

 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     }

 

Extension Method

/// <Summary> /// SparkContainer /// </summary> /// <param name = "helper"> HtmlHelper </param> /// <param name = "tagName"> tag name </param> // <param name = "attributes"> htmlAttributes </param> // <param name = "accessKey"> accessKey </ param> // <returns> </returns> public static 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

Instructions for use:

@using(Html.SparkContainer("div",new { @class="container",custom-attribute = "abcd" })){    @Html.Raw("1234")}

 

If you do not have the permission to access the page, the content will not be rendered to the page. When you have the permission to access the page, the Html generated by the actual rendering is as follows:

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

 

Contact

If you have any questions or suggestions, please contact me at weihanli@outlook.com

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.