ASP.NET Core TagHelpers Concepts

 

1)What is TagHelpers in ASP.NETCore ?


-Using TagHelpers able to render server side code in HTML elements in Razor files.
-Diffrent kind of TagHelpers available for different purposes.

Example:
Anchor TagHelper , Environment TagHelper Etc.
<label asp-for="College.DeptName"></label>

2)What is the difference between WebServer controls and TagHelpers in ASP.NETCore?

-Webserver controlers had a complex lifecycle so hat debuging is bit complex
-TagHelpers doesnot have any life cycle.

-Using WebServer controls you can add functionality to DOM model using ControlID ect..
-TagHelpers rendered as part HTML control , Doesnot add any functionality to DOM model

-WebServer controls rendered on browser so that they can detect the browser
-TagHelpers rendered as part of HTML so that doesn't detect browser

-Using System.ComponentModel webserver control implement the behavior    
-Using TagHelper class agHelpers implement the behavior 

3)What is difference between normal HTML elements and TagHelpers in ASP.NETCore?

-TagHelpers doesn't have any special syntax, TagHelpers target the HTML elements.
-If 'asp-' prefix in the HTML control attribute is the only difference, If 'asp-' prefix attributes removed then it would be like normal HTML control

<label asp-for="College.DeptName"></label>

4)What is the difference between TagHelpers and HTML Helpers in ASP.NETCore?

-Using TagHelpers able to render server side code in HTML elements in Razor files.
-HTML helpers invoked as methods and generating required HTML element razor file.
-TagHelpers not replace the HTML helpers. HTML helpers has replacement for all TagHelpers but not vice - versa

5)What are the advantages of TagHelpers in ASP.NETCore?

-Intelliscense support : all HTML controls not a TagHelpers, So by seeing Intelliscense you can identify Controls which are prefixed @ symbol , If you click on "asp-" will get all the TagHelper attributes

-Development time greatly reduced, Front end developers also easily edit the code with prior knowledge with C#.Net

-TagHelpers rendering server side content with HTML controls so that its more reliable and maintainable.   

6)What is the use of @addTagHelper in ASP.NETCore?  

Adding @addTagHelper on top of the view so that TagHelpers available for the view

ex: @addTagHelpers *,<assembly-name> 

placing '*' (wild card character) prefix is makes all TagHelpers available to view  from imported assembly.
Rather than adding @addTagHelper in each and every view and it _ViewImports.cshtml file 
_ViewImports.cshtml file imports available to all the views in View folder

7)What is @removeTagHelper in ASP.NETCore?

using @removeTagHelper remove already added TagHelpers 

using this able restrict globally added TagHelpers in a view

8)What is the use of opt-out character (!) in ASP.NETCore?

Using opt-out character, you can disable TagHelpers to HTML element level, So that you can not use that element as a TagHelper.
-needs to apply opt-out character for both open and close tag anyway visual studio apply this to closing tag automatically but needs to take care 
<!label asp-for="College.DeptName"> </!label>


9) What is asp-route-* wildcard attribute in ASP.NETCore?

using asp-route-* attribute specify route parameters 

<form asp-action="GeTransport" asp-controller="GetData"  asp-route-rno="5">

10) What is asp-route attribute in ASP.NETCore?

using asp-route attribute specify routename of the route 

11) What is FormTagHelper in ASP.NETCore?

-FormTagHelper used to generate action attribute depends givendata.
-By default predominately generating hidden field for preventing CSRF attacks.

CSRF token name is like "_RequestVerificationToken" 

<form asp-action="GetTransport" asp-controller="GetData"  asp-route-rno="5">

For above syntax generating action attribute like GetData/GetTransport/5

12) What is LabelTagHelper in ASP.NETCore?

-Label TagHelper is used to display caption
-Label TagHelper uses the display attribute 
Ex:
[Display(Name="Your Name")]
public string StudentName {get;set;}
<label asp-for="StudentName"></label>
If property has display attribute with Name parameter it will take name from that if no Display attribute it will render property 

13) What is InputTagHelper in ASP.NETCore?

InputTagHelper handling the user input, User input may differ control to control.

Boolean values are represented with check boxes, Date represented with datepicker like this we have different types of data int, string , password, hiddeninput, email etc..

-This is generating HTML based on attribute and type.

14) What is the ValidationMessage TagHelper in ASP.NETCore? 

- ValidationMessage TagHelper used to display error messages for given property.
-This associated with <span> Tag.

[Required]
[StringLength(50,ErrorMessage = "Allowed name length is 40 characters")]
Public string Name {get; set;}

<span asp-validation-for="Name"></span>

15) What is the ValidationSummary TagHelper in ASP.NETCore? 

 ValidationSummary Taghelper  used to display summary of validation messages in <div> tag.

ValidationSummary  taghelper take 3 kind of parameters as a input

None- Not displaying any error message
Modelonly-Display model errors that are not associated with properties of the class.
All - Display both model and property errors.

Examples:

<div asp-validation-summary = "All" > </div>

if(!ModelState.IsValid)
{
ModelState.AddModelError(string.Empty, "XYZ Error message")
}


Thanks for visiting this blog. How is the content?. Your comment is great gift to my work. Cheers.

No comments:

Post a Comment