1. En el proyecto que marca un nuevo ayudante, de entrada MyEmailTagHelper
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyRazor.Models
{
[HtmlTargetElement("email")]
public class MyEmailTagHelper:TagHelper
{
public string To { get; set; }
public string Subject { get; set; }
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
// Evaluate the Razor content of the email's element body
var body = (await output.GetChildContentAsync()).GetContent();
body = body.Trim();
// Replace <email> with <a>
output.TagName = "a";
// Prepare mailto URL
var to = context.AllAttributes["to"].Value.ToString();
var subject = context.AllAttributes["subject"].Value.ToString();
var mailto = "mailto:" + to;
if (!string.IsNullOrWhiteSpace(subject))
mailto = string.Format("{0}&subject={1}&body={2}", mailto, subject, body);
// Prepare output
output.Attributes.Remove(context.AllAttributes["to"]);
output.Attributes.Remove(context.AllAttributes["subject"]);
output.Attributes.SetAttribute("href", mailto);
output.Content.Clear();
output.Content.AppendFormat("Email {0}", to);
}
}
}
2. Agregar @addTagHelper en el interior ViewImports *, MyRazor, prestar atención a que el segundo parámetro es el nombre de ensamblado en lugar de un espacio de nombres
@using MyRazor
@using MyRazor.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, MyRazor
3. El aumento en la vista del índice dentro de la etiqueta de correo electrónico, considerada proceso de entrada normal incitó
@model MyRazor.Models.RoomViewModel
@{
ViewData["Title"] = "Home Page";
}
@{
var email = "[email protected]";
var subject = "Hello";
}
<email to="@email" subject="@subject" class="btn btn-danger">
Hello! Today is @DateTime.Today
</email>