Write at the beginning
In the previous article, I briefly introduced what is taghelper and how to use it. Next I will briefly introduce the embedded taghelpers that Microsoft has released with Asp.net5. Each of them is:
- Anchortaghelper
- Cachetaghelper
- Environmenttaghelper
- Inputtaghelper
- Labeltaghelper
- Selecttaghelper
- Optiontaghelper
- Textareataghelper
- Validationmessagetaghelper
- Validationsummarytaghelper
- Formtaghelper
- Linktaghelper
- Scripttaghelper
Anchortaghelper
This taghelper is applied to all anchor elements <a>, it has properties:
- Asp-action
Specifies the action method name.
- Asp-controller
Specifies the controller name.
- Asp-fragment
Specifies the URL fragment name.
- Asp-host
Specifies the host name of the access.
- Asp-protocol
Specify the access protocol, such as HTTP or HTTPS.
- Asp-route
Specifies the route name.
Eventually the Taghelper will be parsed into an anchor element with an HREF attribute, which is generated based on the values of these attributes.
<a asp-action= "Create" >create new</a>
Cachetaghelper
Applies on the cache element, using the IMemoryCache instance to cache the contents of the cache element in the memory of the current process. It supports the following properties:
- Vary-by
String, Taghelper will cache the content based on that value, which is used to generate the cache key.
- Vary-by-header
A string type that specifies the request header, which can only specify a single header name, Taghelper will cache the content based on that value, which is used to generate the cache key.
- Vary-by-query
A string type that specifies the request parameter, separated by commas when there are multiple request parameters. Taghelper will cache the content based on it, and this value will be used to generate the cache key.
- Vary-by-route
A string type that specifies the route data parameter, separated by commas when there are multiple route data parameters. Taghelper will cache the content based on it, and this value will be used to generate the cache key.
- Vary-by-cookie
String type, specifying the name of the cookie, separated by commas when there are multiple cookies. Taghelper will cache the content based on these cookie names, which will be used to generate the cache key.
- Vary-by-user
BOOL type, specifies whether the cache is used for each logged-on user, and the user information is used to generate cache key.
- Expires-on
A datetime type that specifies when the cache expires.
- Expires-after
The timespan type, which specifies how long the cache expires, and the time is timed from the join cache.
- Expires-sliding
A TimeSpan type that specifies how much time expires after the cache has not been used.
- Priority
The enum type, with the following possible values:
- Cachepreservationpriority.low
- Cachepreservationpriority.normal
- Cachepreservationpriority.high
- Cachepreservationpriority.neverremove
The cache stored in the IMemoryCache instance is limited by the current available memory. If the server is going to overflow memory, the memory cache clears the cache to release the content. At this point, we can use this property to specify the priority of the current cache, so that when there is a release, we consider releasing the cache with low priority.
Environmenttaghelper
Apply the conditional render different content on the environment element, depending on the settings of the different names. It supports the following properties:
- Names
Specifies the environment name, separated by commas when there are multiple times. The basis for judging here is that the value read, IHostingEnvironment
EnvironmentName
matches the names in the environment element, and when the match is on, it will render the content inside, or remove the environment element.
In many cases, we want to use a set of configuration information in the development environment, in the production environment is another set, this time we need to use conditional judgment statement, but in the new version of MVC, using the environmenttaghelper provided by the environment element tag can be, Examples are as follows:
<environment names= "Development" > <script src= "~/lib/jquery/jquery.js" ></script> <script src= " ~/lib/bootstrap/js/bootstrap.js "></script> <script src=" ~/lib/hammer.js/hammer.js "></script > <script src= "~/lib/bootstrap-touch-carousel/js/bootstrap-touch-carousel.js" ></script></ Environment><environment names= "Staging,production" > <script src= "//ajax.aspnetcdn.com/ajax/jquery/ Jquery-1.10.2.min.js "asp-fallback-src=" ~/lib/jquery/jquery.min.js "asp-fallback-test=" window.jQuery "> </script> <script src="//ajax.aspnetcdn.com/ajax/bootstrap/3.0.0/bootstrap.min.js "Asp-fal Lback-src= "~/lib/bootstrap/js/bootstrap.min.js" asp-fallback-test= "Window.jquery" > </script> < Script src= "//ajax.aspnetcdn.com/ajax/hammer.js/2.0.4/hammer.min.js" asp-fallback-src= "~/lib/hammer.js/hammer. JS "asp-fallback-test=" window. Hammer "> </script> <script src= "//ajax.aspnetcdn.com/ajax/bootstrap-touch-carousel/0.8.0/js/ Bootstrap-touch-carousel.js "asp-fallback-src=" ~/lib/bootstrap-touch-carousel/js/bootstrap-touch-carousel.js " asp-fallback-test= "window. Zepto "> </script></environment>
In the above code, we are determined that if the development environment uses the local JS file, otherwise (staging or production environment) will load the CDN file first.
Inputtaghelper
This taghelper is applied to the input element, and as with textboxforhtml in Htmlhelpers, this taghelper generates an INPUT element that is bound to a field in the model. It supports attributes:
Asp-for is used to specify which field to bind the model to Taghelper, and many other taghelpers also have this property.
Asp-format is used to set the display format, which is typically used to set format for values of currency, date, and time types, for example, birthday is a field of a date type in model.
<input asp-for= "Birthday" asp-format= "{0:yyyy-mm-dd}"/>
Note: The Asp-for type modelexpression is a newly defined class in ASP. NET MVC 6 with a constructor with a character type parameter that specifies the name of the field in the model. We can also assign an inline object to the asp-for, for example:
<input asp-for= "Address.street" type= "text"/>
Labeltaghelper
Like the Htmlextension.labelfor function, it has only one property asp-for, which specifies a field in the binding model. It works on the label element.
<label asp-for= "Birthday"/>
Selecttaghelper
The Selecttaghelper function is supported on the SELECT element and supports the Asp-for and Asp-items properties.
Asp-for is used to bind a field in the model, as we described above.
Asp-items, which is used to specify the option collection of the Select element, whose value type is ienumerable<selectlistitem>.
<select asp-for= "Country" asp-items= "viewbag.countries" >
If you want to add a default selected item in Select, we can do this:
<select asp-for= "Country" asp-items= "viewbag.countries" > <option selected= "selected" Value= "" > Choose country</option></select>
We can assign any type of Ienumerable<selectlistitem> instance to Asp-items, which may be a variable or an attribute of an instance.For example:
@{ selectlistitem[] items = { new SelectListItem () {text = "Item 1"}, new SelectListItem () {text = "ite M 2 "} };} <select asp-for= "Country" asp-items= "items" ></select>
Optiontaghelper
Applied on the option element, used with the Select element, is often used to read the option element information without altering the element content. The only possible modification is that, in some cases, the selected state of option is set to "selected" based on the father select element.
<select asp-for= "Country" asp-items= "viewbag.countries" > <option selected= "selected" Value= "" > Choose country</option></select>
Textareataghelper
Applied on the TEXTAREA element, only one attribute asp-for is currently supported,
<textarea asp-for= "Information" ></textarea>
Validationmessagetaghelper
As with validationmessagefor in HtmlHelper, this taghelper is used to display validation failure information. It is applied to a SPAN element, and only one property, Asp-validation-for, is used to specify the object being validated----a field in the model.
<input asp-for= "Birthday" asp-format= "{0:yyyy-mm-dd}"/><span asp-validation-for= "Birthday"/>
Validationsummarytaghelper
Like the HtmlHelper extended ValidationSummary, it is a summary of information used to validate errors. It supports only one attribute, Asp-validation-summary, with the following values:
- None
Do not display any verification information
- Modelonly
Only model error messages, not property error messages, are displayed
- All
Show All information
It is applied to the DIV element,
<div class= "Validation" asp-validation-summary= "Modelonly"/>
Formtaghelper
Like the BeginForm in HtmlHelper, it is used to generate a form element that is applied on the form element and supports the following properties:
- Asp-action
- Asp-controller
- Asp-anti-forgery
<form asp-action= "Formsave" asp-controller= "Home" asp-anti-forgery= "true" >
Linktaghelper
Applies to the link element, supporting alternate style files. It has the following properties:
- Href
Specifies the link address of the style resource.
- Asp-href-include
Specifies all the style file path formats that need to be loaded, separating each one with commas when there are multiple, and the path here is relative to the wwwroot relative path in the application.
- Asp-href-exclude
Specify the style file path format that does not need to be loaded, and when there are multiple, separate each one with commas; the path here is relative to the wwwroot relative path in the application.
- Asp-fallback-href
Specifies the alternate resource link address.
- Asp-fallback-href-include
Specifies all alternate style file path formats that need to be loaded, separated by commas when there are more than one, and the path here is relative to the wwwroot relative path in the application.
- Asp-fallback-href-exclude
Specify the alternate style file path format that does not need to be loaded, separating each one with commas when there are multiple, and the path here is relative to the wwwroot relative path in the application.
- Asp-fallback-test-class
The style name used to detect the load failure.
- Asp-fallback-test-property
The test properties used to detect the failure of a resource load.
- Asp-fallback-test-value
The test value used to detect the failure of the resource load.
- Asp-file-version
A bool value that specifies whether file version information needs to be added to the URL address.
For example, in the following example, when from the network (http://ajax.aspnetcdn.com/ajax/bootstrap-touch-carousel/0.8.0/css/ BOOTSTRAP-TOUCH-CAROUSEL.CSS) loading the style file fails, load the local appropriate style file (~/lib/bootstrap-touch-carousel/css/ BOOTSTRAP-TOUCH-CAROUSEL.CSS). Determines whether the style file on the network is loaded successfully by detecting whether the display property in the style class carousel-caption is none.
<link rel= "stylesheet" href= "//ajax.aspnetcdn.com/ajax/bootstrap-touch-carousel/0.8.0/css/ Bootstrap-touch-carousel.css "asp-fallback-href=" ~/lib/bootstrap-touch-carousel/css/ Bootstrap-touch-carousel.css "asp-fallback-test-class=" carousel-caption "asp-fallback-test-property=" display " Asp-fallback-test-value= "None"/>
Scripttaghelper is applied to the script element, like Linktaghelper, it also has fallback function, but it is not judged by the class style, but to detect whether an object exists, to determine whether the default JS file is loaded successfully. It supports the following properties:
- Src
Specifies the JS source address to load.
- Asp-src-include
Specifies the JS file format to load, separated by commas when there are multiple file formats. The file path here is relative to the program Webroot.
- Asp-src-exclude
Specifies the JS file format that does not need to be loaded, separated by commas when there are multiple file formats. The file path here is relative to the program Webroot.
- Asp-fallback-src
Specifies the alternate JS source address.
- Asp-fallback-src-include
Specifies the alternate JS file format that needs to be loaded, separated by commas when there are multiple file formats. The file path here is relative to the program Webroot.
- Asp-fallback-src-exclude
Specifies the alternate JS file format that does not need to be loaded, separated by commas when there are multiple file formats. The file path here is relative to the program Webroot.
- Asp-fallback-test
Specifies the object used to detect the success of JS loading
- Asp-file-version
A bool value that specifies whether file version information needs to be added to the URL address.
<script src= "//ajax.aspnetcdn.com/ajax/jquery/jquery-1.10.2.min.js" asp-fallback-src= "~/lib/jquery/ Jquery.min.js " asp-fallback-test=" Window.jquery "></script>
Write at the end
The above simple introduction of the embedded Taghelpers properties, some of the content I find from the Internet, you can use a specific taghelper time to try the properties of the inside, it is more conducive to their understanding and correct use.
Said so many embedded taghelper, in the next I will describe how to write a custom taghelper, to achieve their own functions required, please look forward to!
The things about Taghelper (2)