最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

asp.net core新特性之TagHelper標(biāo)簽助手

 更新時(shí)間:2017年07月11日 14:23:26   作者:BillMing  
這篇文章主要為大家詳細(xì)介紹了asp.net core新特性之TagHelper標(biāo)簽助手的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

今天開(kāi)始,我就來(lái)說(shuō)說(shuō)asp.net core的新特性,今天就說(shuō)說(shuō)TagHelper標(biāo)簽助手。雖然學(xué)習(xí).net,最有幫助的就是microsoft的官方說(shuō)明文檔了,里面把一些使用說(shuō)明都寫(xiě)的非常清楚,但奈何.net core放入文檔微軟還沒(méi)來(lái)得及翻譯,對(duì)于英文不好的人來(lái)說(shuō)簡(jiǎn)直就是看的艱辛。所以今天就來(lái)學(xué)習(xí)學(xué)習(xí)這標(biāo)簽助手,和博客園大佬分享分享經(jīng)驗(yàn)。

  想看Microsoft官方文檔和Git項(xiàng)目的可以直接點(diǎn)擊以下傳送門(mén)~~

  asp.net core 官方文檔

  asp.net core github項(xiàng)目

  說(shuō)起TagHelper給我的印象,有點(diǎn)像asp.net form當(dāng)中的服務(wù)器端控件,又有點(diǎn)像Angular或者Vue當(dāng)中的“組件”的后端實(shí)現(xiàn)版本。用戶可以將一組html標(biāo)簽集合轉(zhuǎn)換為一個(gè)自定義標(biāo)簽,實(shí)現(xiàn)了html代碼的復(fù)用。

  那么正文開(kāi)始~~

  首先,我們需要安裝一個(gè)vs2017插件:Razor Language Services。這個(gè)插件能在html中智能提示用戶自定義的標(biāo)簽助手。

  https://marketplace.visualstudio.com/items?itemName=ms-madsk.RazorLanguageServices

創(chuàng)建一個(gè)asp.net core項(xiàng)目

使用微軟定義的標(biāo)簽助手,在安裝了插件后,使用標(biāo)簽助手的標(biāo)簽會(huì)進(jìn)行高亮顯示

上圖中environment、link、a標(biāo)簽均使用了標(biāo)簽助手實(shí)現(xiàn)各自的功能

<a asp-area="" asp-controller="Home" asp-action="Index" class="navbar-brand">taghelpersample</a>

a標(biāo)簽中通過(guò)使用asp-controller,asp-action自定義屬性來(lái)實(shí)現(xiàn)路由訪問(wèn)。

這時(shí)有人會(huì)說(shuō),我也可以使用@Html類(lèi)來(lái)實(shí)現(xiàn)相同功能,為什么需要使用TagHelper?

@Html.ActionLink("taghelpersample", "Index", "Home",null, new { Class = "navbar-brand" })

確實(shí),使用@Html幫助類(lèi)我們能實(shí)現(xiàn)相同的功能,但是使用標(biāo)簽助手的方式不是更加符合html的標(biāo)簽語(yǔ)法嗎,對(duì)于強(qiáng)迫癥程序員簡(jiǎn)直就是福音~(yú)~。而且對(duì)于標(biāo)簽的原有屬性的添加例如class,標(biāo)簽助手的使用也更加方便。

<!--標(biāo)簽助手版form-->
<form asp-controller="Home" asp-action="Index" class="form-horizontal" method="post">

</form>
<!--Html幫助類(lèi)版form-->
@using (Html.BeginForm("Index", "Home", FormMethod.Post,, new { Class = "form-horizontal" }))
{

}


此外,標(biāo)簽助手的另外一個(gè)特色就是可以自定義,具體步驟如下:

(1)創(chuàng)建派生自TagHelper類(lèi)的Class

 //類(lèi)會(huì)默認(rèn)轉(zhuǎn)換為<text-collection></text-collection>
   public class TextCollectionTagHelper:TagHelper
 {
  public override void Process(TagHelperContext context, TagHelperOutput output)
  {
   base.Process(context, output);
  }
 }


(2)設(shè)置屬性與基本類(lèi)

public string Color { get; set; }

  public override void Process(TagHelperContext context, TagHelperOutput output)
  {
   output.TagName = "div";
   output.Attributes.Add("style", "color:" + Color);
   var text = "Hello,World";
   var h1 = new TagBuilder("h1");
   var h2 = new TagBuilder("h2");
   var h3 = new TagBuilder("h3");
   var h4 = new TagBuilder("h4");
   var h5 = new TagBuilder("h5");
   var h6 = new TagBuilder("h6");
   h1.InnerHtml.Append(text);
   h2.InnerHtml.Append(text);
   h3.InnerHtml.Append(text);
   h4.InnerHtml.Append(text);
   h5.InnerHtml.Append(text);
   h6.InnerHtml.Append(text);
   output.Content.AppendHtml(h1);
   output.Content.AppendHtml(h2);
   output.Content.AppendHtml(h3);
   output.Content.AppendHtml(h4);
   output.Content.AppendHtml(h5);
   output.Content.AppendHtml(h6);
  }


(3)在_ViewImports.cshtml導(dǎo)入類(lèi)命名空間

@addTagHelper *,taghelpersample

(4)在cshtml中使用標(biāo)簽助手

<text-collection color="red"></text-collection>
<text-collection color="blue"></text-collection>
<text-collection color="#666"></text-collection>

(5)調(diào)試效果

OK,今天關(guān)于TagHelper就分享到這。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

重庆市| 霍林郭勒市| 辉县市| 吉安县| 湾仔区| 南郑县| 吉首市| 安龙县| 边坝县| 孙吴县| 吐鲁番市| 崇左市| 康平县| 屯昌县| 海城市| 博白县| 新巴尔虎右旗| 镶黄旗| 齐河县| 定结县| 崇文区| 昭通市| 山丹县| 司法| 临邑县| 新龙县| 板桥市| 阳泉市| 林州市| 项城市| 梁平县| 香格里拉县| 沈阳市| 延吉市| 揭西县| 贺兰县| 额济纳旗| 泗阳县| 嘉荫县| 和平区| 塔城市|