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