It's been a long time since I published an article in the blog Park, and today I'll summarize how to add a custom HTML helper method to ASP. We're now designing this one. Currently, a custom HTML method is used to render a normal IMG tag. Go directly to the topic:
First, let's take a look at the code in HTML for an ordinary IMG tag:
This is the result from the server-side output to the browser after rendering, so let's implement it now.
In the first step, we need to define an extension method, which is used to render the IMG element with the following code:
Using system;using system.collections.generic;using system.linq;using system.web;using system.web.mvc;//Note: 1th, The namespace must be System.Web.Mvc.Htmlnamespace system.web.mvc.html{//2nd, the class name can be any name, but must be static class public static classes Createimageex Tensions {/* public static mvchtmlstring Image (this htmlhelper HTML, string id,string url,string width,s Tring height,dictionary<string,object> attributes) {Tagbuilder taghelper = new Tagbuilder ("image" ); Taghelper.generateid (ID); Taghelper.mergeattribute ("src", url); Taghelper.mergeattribute ("width", width); Taghelper.mergeattribute ("height", height); if (attributes!=null&&attributes. count>0) taghelper.mergeattributes (attributes); Return Mvchtmlstring.create (Taghelper.tostring ()); }*///Third, the name of the extension method can use any name, but this method must meet the following two points://01. Must be extended from HtmlHelper class;//02. The return value type of the method must be: mvchtmlstring public static MVCHTmlstring createimage (this htmlhelper HTML, string ID, string src, string width, string height,string cssClass, Dictionary <string, object> attributes) {Tagbuilder taghelper = new Tagbuilder ("img"); Taghelper.generateid (ID); Taghelper.mergeattribute ("src", SRC); Taghelper.mergeattribute ("width", width); Taghelper.mergeattribute ("height", height); if (Attributes! = null && attributes. Count > 0) taghelper.mergeattributes (attributes); Return Mvchtmlstring.create (taghelper.tostring (tagrendermode.selfclosing)); } }}
Here are three notable points to note:
1. It is essential that the namespace be System.Web.Mvc.Html;
2. The class name can be any name, but it must be a static class. In fact, the extension method must also be defined in a static class;
3. The name of the extension method can use any name, but it must be extended from the HtmlHelper class, and the return value type of the method must be: mvchtmlstring.
Of course, the parameter list and implementation code of the method can be defined and written according to the actual situation, I am just a simple way to tidy up the steps, my implementation code is very simple, just to explain the steps. In fact, the class name and method name should also be named with the "actual meaning" name, we can follow the MVC framework of the "Convention" to name, such as the class name Imageextensions, the method named image, and so on. The other thing to note is that we can actually define overloaded methods to meet our other needs, such as the strongly typed version, which can refer to the MVC Framework's HTML helper method.
With the three points we just said, the next step is to call the HTML helper method we just defined in view, such as the following code in the index.cshtml file:
<div> @Html. CreateImage ("Img01", Url.content ("~/content/images/img_9084_2"), "551px", "787px", "imgcls ", NULL) </div>
This is exactly the same as our usual HTML-assisted approach to invoking the MVC framework.
How to add custom HTML helper methods in ASP.