当我们新建了一个.Net Core类型的Project时,我们会看到页面上有类似于这样的代码:

 

ASP.Net Core-TagHelpers

当我们运行项目,查看源代码会发现,浏览器中的就是Html代:

ASP.Net Core-TagHelpers

那么,为什么我们在页面写的代码会转化为html标签呢?接下来我们就来探索这个秘密。

当我们在vs中把鼠标放到这样的元素上时发现这样的元素都是一个Microsoft.AspNetCore.Mvc.TagHelpers命名空间下的对象,我们通过Reflector查看a标签的对象AnchorTagHelper

[HtmlTargetElement("a", Attributes="asp-action"), HtmlTargetElement("a", Attributes="asp-controller"), HtmlTargetElement("a", Attributes="asp-area"), HtmlTargetElement("a", Attributes="asp-fragment"), HtmlTargetElement("a", Attributes="asp-host"), HtmlTargetElement("a", Attributes="asp-protocol"), HtmlTargetElement("a", Attributes="asp-route"), HtmlTargetElement("a", Attributes="asp-all-route-data"), HtmlTargetElement("a", Attributes="asp-route-*")]
public class AnchorTagHelper : TagHelper
{
    // Fields
    private IDictionary<string, string> _routeValues;
    private const string ActionAttributeName = "asp-action";
    private const string AreaAttributeName = "asp-area";
    private const string ControllerAttributeName = "asp-controller";
    private const string FragmentAttributeName = "asp-fragment";
    private const string HostAttributeName = "asp-host";
    private const string Href = "href";
    private const string ProtocolAttributeName = "asp-protocol";
    private const string RouteAttributeName = "asp-route";
    private const string RouteValuesDictionaryName = "asp-all-route-data";
    private const string RouteValuesPrefix = "asp-route-";

    // Methods
    public AnchorTagHelper(IHtmlGenerator generator);
    public override void Process(TagHelperContext context, TagHelperOutput output);

    // Properties
    [HtmlAttributeName("asp-action")]
    public string Action { get; set; }
    [HtmlAttributeName("asp-area")]
    public string Area { get; set; }
    [HtmlAttributeName("asp-controller")]
    public string Controller { get; set; }
    [HtmlAttributeName("asp-fragment")]
    public string Fragment { get; set; }
    protected IHtmlGenerator Generator { [CompilerGenerated] get; }
    [HtmlAttributeName("asp-host")]
    public string Host { get; set; }
    public override int Order { get; }
    [HtmlAttributeName("asp-protocol")]
    public string Protocol { get; set; }
    [HtmlAttributeName("asp-route")]
    public string Route { get; set; }
    [HtmlAttributeName("asp-all-route-data", DictionaryAttributePrefix="asp-route-")]
    public IDictionary<string, string> RouteValues { get; set; }
    [HtmlAttributeNotBound, ViewContext]
    public ViewContext ViewContext { get; set; }
}
View Code

相关文章:

  • 2021-08-29
  • 2021-07-04
  • 2021-06-21
  • 2023-01-17
  • 2021-11-14
  • 2021-05-27
  • 2021-05-26
猜你喜欢
  • 2021-10-23
  • 2021-07-04
  • 2021-10-01
  • 2022-12-23
  • 2022-12-23
  • 2021-10-04
  • 2021-05-27
相关资源
相似解决方案