Custom extension container based on HtmlHelper

Source: Internet
Author: User
Tags tagname

Custom extension Containerintro based on HtmlHelper

Part of an asp. net mvc-based permission control system for control over UI layer data rendering, based on HtmlHelper extension components

Code

Authorization control system based on asp. NET MVC example code: Https://github.com/WeihanLi/AccessControlDemo

Permission Control Core Code: Https://github.com/WeihanLi/AccessControlDemo/tree/master/AccessControlHelper

Sparkcontainer code:

1  public classsparkcontainer:idisposable2     {3         Private ReadOnly string_tagname;4         Private ReadOnlyViewContext _viewcontext;5         Private ReadOnly BOOL_canaccess;6         Private BOOL_disposed;7 8         Private ReadOnly string_content;9 Ten          publicSparkcontainer (viewcontext viewcontext,stringtagName,BOOLCanaccess =true) one         { a_viewcontext =viewcontext; -_tagname =tagName; -_canaccess =canaccess; the             if(!_canaccess) -             { -_content = (_viewcontext.writer asstringwriter). Getstringbuilder (). ToString (); -             } +         } -  +          public voidDispose () a         { atDispose (true); -Gc. SuppressFinalize ( this); -         } -  -         protected Virtual voidDispose (BOOLDisposing) -         { in             if(!_disposed) -             { to_disposed =true; + Endshopcontainer (); -             } the         } *  $          public voidEndshopcontainer ()Panax Notoginseng         { -             if(!_canaccess) the             { +(_viewcontext.writer asstringwriter). Getstringbuilder (). Clear (). Append (_content); a             } the             Else +             { -_viewcontext.writer.write ("</{0}>", _tagname); $             } $         } -}

Extension methods

 /// <summary>        ///Sparkcontainer/// </summary>        /// <param name= "helper" >HtmlHelper</param>        /// <param name= "tagName" >Label name</param>        /// <param name= "attributes" >htmlattributes</param>        /// <param name= "accessKey" >AccessKey</param>        /// <returns></returns>         public StaticSparkcontainer Sparkcontainer ( thisHtmlHelper helper,stringtagName,Objectattributes =NULL,stringAccessKey ="")        {            // ...            returnsparkcontainerhelper (helper, tagName, htmlhelper.anonymousobjecttohtmlattributes (attributes),        Displaystrategy.iscontrolcanaccess (accessKey)); }        Private StaticSparkcontainer Sparkcontainerhelper ( thisHtmlHelper helper,stringtagName, IDictionary<string,Object> Attributes =NULL,BOOLCanaccess =true)        {            // ...Tagbuilder Tagbuilder =NewTagbuilder (tagName); if(canaccess) {tagbuilder.mergeattributes (attributes); Helper.            ViewContext.Writer.Write (tagbuilder.tostring (tagrendermode.starttag)); }            return NewSparkcontainer (helper.        viewcontext, tagName, canaccess); }

Use

Instructions for Use:

@using (html.sparkcontainer ("div",new {@class ="container " " ABCD " }) {    @Html. Raw ("1234")}

When no access is granted, the content is not rendered to the page, and the actual rendering of the generated Html is as follows when Access is granted:

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

Contact

If you have any questions or suggestions, please feel free to contact me [email protected]

Custom extension container based on HtmlHelper

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.