title
stringlengths 3
46
| content
stringlengths 0
1.6k
|
---|---|
17:591 | ... |
17:592 | asp-route-{action method parametern}=`valuen`> |
17:593 | put anchor text here |
17:594 | </a> |
17:595 | |
17:596 | Below is an example of its usage: |
17:597 | <a asp-controller=`Home` asp-action=`Index`> |
17:598 | Back Home |
17:599 | </a> |
17:600 | |
17:601 | The HTML created is: |
17:602 | <a href=`Home/Index`> |
17:603 | Back Home |
17:604 | </a> |
17:605 | |
17:606 | It might appear that there is almost no advantage in using the tag helper. However, this isn’t true! The advantage is that whenever the routing rules change, the tag helper automatically updates the href that it generates to conform to the new routing rules. |
17:607 | A similar syntax is added to the form tags: |
17:608 | <form asp-controller=`{controller name}` |
17:609 | asp-action=`{action method name}` |
17:610 | asp-route-{action method parameter1}=`value1` |
17:611 | ... |
17:612 | asp-route-{action method parametern}=`valuen` |
17:613 | ... |
17:614 | > |
17:615 | ... |
17:616 | |
17:617 | The script tag is enhanced with attributes that allow us to fall back to a different source if the download fails. Typical usage is to download scripts from some cloud service to optimize the browser cache and to fall back to a local copy of the script if there is a failure. The following code uses the fallback technique to download the bootstrap JavaScript file: |
17:618 | <script src=`https://stackpath.bootstrapcdn.com/ |
17:619 | bootstrap/4.3.1/js/bootstrap.bundle.min.js` |
17:620 | asp-fallback-src=`~/lib/bootstrap/dist/js/bootstrap.bundle.min.js` |
17:621 | asp-fallback-test=`window.jQuery && window.jQuery.fn && window.jQuery.fn.modal` |
17:622 | crossorigin=`anonymous` |
17:623 | integrity=`sha384-xrRywqdh3PHs8keKZN+8zzc5TX0GRTLCcmivcbNJWm2rs5C8PRhcEn3czEjhAO9o`> |
17:624 | </script> |
17:625 | |
17:626 | asp-fallback-test contains a JavaScript test that verifies whether the download succeeded. In the preceding example, the test verifies whether a JavaScript object has been created. |
17:627 | All HTML tags that admit an src attribute, that is, the img and the script tags, can be added as an asp-append-version attribute set to true. The asp-append-version attribute set to true doesn’t change the syntax of the img and script tags; it just adds a hash to the src query string to prevent cashing each time the image or script file changes. Here is an example: |
17:628 | <img src=`~/images/myImage.png` asp-append-version=`true`> |
17:629 | |
17:630 | Which is rendered as: |
17:631 | <img src=`/images/myImage.png?v=kM_dqr9NVtnMdsM2MUgdskVVFD`> |
17:632 | |
17:633 | The hash passed in the v query parameter is computed from the content of the image file, so it changes whenever the image changes, thereby preventing the browser from rendering an old cached copy of the image. |
17:634 | The ~/ symbol is not a feature specific to the img tag helper and, instead, a Razor native feature you can use in all links contained in any tag of a Razor file. It stands for the application root. It is not equivalent to the HTML / symbol that stands for the root of the domain, because ASP.NET Core applications can also be placed in subfolders of the domain. So ~/ translates as / only when the application is placed in the domain root; otherwise, it translates as /{application subfolder name}/. |
17:635 | The environment tag can be used to select different HTML for different environments (development, staging, and production). Its typical usage is selecting the debug versions of JavaScript files during development, as shown in this example: |
17:636 | <environment include=`Development`> |
17:637 | @*development version of JavaScript files*@ |
17:638 | </environment> |
17:639 | <environment exclude=`Development`> |
17:640 | @*development version of JavaScript files *@ |
17:641 | </environment> |
17:642 | |
17:643 | There is also a cache tag, which caches its content in memory to optimize rendering speed: |
17:644 | <cache> |
17:645 | @* heavy to compute content to cache *@ |
17:646 | </cache> |
17:647 | |
17:648 | By default, content is cached for 20 minutes, but the tag has attributes that must be defined when the cache expires, such as expires-on=`{datetime}`, expires-after=`{timespan}`, and expires-sliding=`{timespan}`. Here, the difference between expires-sliding and expires-after is that, in the second attribute, the expiration time count is reset each time the content is requested. The vary-by attribute causes the creation of a different cache entry for each different value passed to vary-by. There are also attributes such as vary-by-header, which creates a different entry for each different value assumed by the request header specified in the vary-by-cookie attribute, and so on. |
17:649 | All input tags – that is, textarea, input, and select – have an asp-for attribute that accepts a properties path rooted in the view’s ViewModel as their value. For instance, if the view has a Person ViewModel, we may have something like this: |
17:650 | <input type=`text` asp-for`Address.Town`/> |
17:651 | |
17:652 | The first thing the preceding code does is assign the value of the Town nested property to the value attribute of the input tag. In general, if the value is not a string, it is converted into a string using the current request culture. |
17:653 | However, it also sets the name of the input field to Address.Town and the id of the input field to Address_Town. This is because dots are not allowed in tag ids. |
17:654 | A prefix can be added to these standard names by specifying it in ViewData.TemplateInfo.HtmlFieldPrefix. For instance, if the previous property is set to MyPerson, the name becomes MyPerson.Address.Town. |
17:655 | If the form is submitted to an action method that has the same Person class as one of its parameters, the name of Address.Town that’s given to the input field will cause the Town property of this parameter to be filled with the input field. In general, the string contained in the input field is converted into the type of property it has been matched with, using the current request culture. Summing this up, the names of input fields are created in such a way that a complete Person model can be recovered in the action method when the HTML page is posted. |
17:656 | The same asp-for attribute can be used in a label tag to cause the label to refer to the input field with the same asp-for value. |
17:657 | The following code is an example of an input/label pair: |
17:658 | <label asp-for=`Address.Town`></label |
17:659 | <input type=`text` asp-for=`Address.Town`/> |
17:660 | |
17:661 | When no text is inserted into the label, the text shown in the label is taken from a Display attribute that decorates the property (Town, in this example), if any; otherwise, the name of the property is used. |
17:662 | If span or div contains an asp-validation-for=`Address.Town` error attribute, then validation messages concerning the Address.Town input will be inserted automatically inside that tag. The validation framework will be described in the Understanding the connection between ASP.NET Core MVC and design principles section. |
17:663 | It is also possible to automatically create a validation error summary by adding the attribute that follows a div or a span: |
17:664 | asp-validation-summary=`ValidationSummary.{All, ModelOnly}` |
17:665 | |
17:666 | If the attribute is set to ValidationSummary.ModelOnly, only messages that are not associated with specific input fields will be shown in the summary, while if the value is ValidationSummary.All, all error messages will be shown. |
17:667 | The asp-items attribute can be applied to any select tag to automatically generate all the select options. It must be passed an IEnumerable<SelectListItem>, where each SelectListItem contains both the text and value of an option. SelectListItem also contains an optional Group property that you can use to organize the options shown in select into groups. |
17:668 | Here is an example of how to use asp-items: |
17:669 | ... |
17:670 | @{ |
17:671 | var choices = new List<SelectListItem> |
17:672 | { |
17:673 | new SelectListItem {Value=`value1`, Text=`text1`, Group=`group1`}, |
17:674 | new SelectListItem {Value=`value2`, Text=`text2`, Group=`group1`} |
17:675 | ... |
17:676 | new SelectListItem {..., Group=`group2`} |
17:677 | ... |
17:678 | } |
17:679 | } |
17:680 | <select asp-for=`MyProperty` asp-items=`choices`> |
17:681 | <option value=``>Select a value</option> |
17:682 | </select> |
17:683 | ... |
17:684 | |
17:685 | When added, option tags are placed before the ones generated by asp-items. |
17:686 | The next topic shows how to reuse view code. |
17:687 | Reusing view code |
17:688 | ASP.NET Core MVC includes several techniques for reusing view code, with the most important being the layout page. |
17:689 | In each web application, several pages share the same structure, for instance, the same main menu or the same left or right bar. In ASP.NET Core, this common structure is factored out in views called layout pages/views. |
17:690 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.