DotNetSlackers: ASP.NET News for lazy Developers

Wednesday, October 7, 2015

DOT NET MVC 6 Tag Helpers

Taylor Mullen was here to present the label assistants that will be added to ASP.NET MVC 6, fundamentally to supplant HTML partners. Label aides are html tag-like bits of C# code. Since they are basically C#, you can get every one of the advantages of the IDE, for example, IntelliSense and refactoring.

Out of the container, there will be label assistants for structures, names, inputs, that comprehend extra properties. For instance, <label asp-for="Email"> will carry on like @Html.LabelFor(m => m.Email).

The <environment> tag can shroud or show parts of the page in light of environment variables, for example, investigate mode.

A typical perception is that these label aides are doing on the server side what Angular does on the customer side.

Label aide source code can as of now be found in the MVC source code repo:

https://github.com/aspnet/Mvc/tree/dev/src/Microsoft.AspNet.Mvc.TagHelpers

To start utilizing label aides as a part of all perspectives, they can be added to _ViewImports.cshtml with a couple utilizing and @AddTagHelpers orders.

With a specific end goal to fabricate a label aide, you should do nothing more than actualize ITagHelper. This interface has an Order perused just property, and a ProcessAsync system that does the genuine work.

public class EmailTagHelper: TagHelper {
  public string MailTo {get;set;}
  public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) {
    output.TagName = "a";
    var email = MailTo + "@contoso.com";
    output.Attributes["href"] = "mailto:" + email;
    output.Content.SetContent(MailTo);

    return base.ProcessAsync(context, output);
  }
}
This can then be used as follows:
<email mail-to="support"><email/>
 
Next, Taylor demonstrated to fabricate a markdown label assistant. This one 
uses the label's substance as the information for the markdown. Since Razor 
is currently completely offbeat, he needs to understand that substance utilizing 
anticipate context.GetChildContent(). 

After that, Taylor indicated how see segments can actualize something like 
Orchard's shapes, utilizing label aides for more pleasant markup. His label 
assistant is getting IViewComponentHelper as a constructor-infused reliance, 
and after that utilizing that to conjure his shape-like perspective segment. 
 
He additionally needed to do property infusion for his perspective connection.
The entire thing is somewhat more unpredictable than Orchard's shapes, however
he get full IntelliSense on the label assistant. It additionally makes it 
conceivable to have discretionary C# expressions in property estimations.
The included many-sided quality included appears as though it could without 
much of a stretch be settled with a decent base class. 

Taylor then demonstrated to change his markdown label aide into a label 
characteristic aide, with the goal that you can do <ul markdown> and have the 
label's substance be sustained into the markdown parser utilizing a 
[HtmlTargetElement("*", Attributes("markdown")] quality. 

In the event that you need to conceal a property from the label aide, you can 
utilize [HtmlAttributeNotBound]. 

On account of the way label assistants are architected, as bits of C# that 
improve the perspective code, it is conceivable to have more than one label
aide focusing on the same HTML component. Taylor showed that by applying a 
Bold and the markdown label assistant that he made some time recently.
This clarifies why the label partner interface has an Order property. 

No comments:

Post a Comment