【移動(dòng)應(yīng)用開發(fā)技術(shù)】ASP.NET中TagHelper如何使用_第1頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】ASP.NET中TagHelper如何使用_第2頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】ASP.NET中TagHelper如何使用_第3頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】ASP.NET中TagHelper如何使用_第4頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】ASP.NET中TagHelper如何使用_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

【移動(dòng)應(yīng)用開發(fā)技術(shù)】ASP.NET中TagHelper如何使用

在新版的MVC6中,微軟提供了強(qiáng)大的TagHelper功能,以便讓我們擺脫如下的臃腫代碼:@Html.LabelFor(model

=>

model.FullName)

@Html.EditFor(model

=>

model.FullName)

@Html.ValidationMessageFor(model

=>

model.FullName)@addTagHelper

"*,

Microsoft.AspNet.Mvc.TagHelpers"

/*

這里需要首先引用TagHelper所在的命名空間

*/

<label

asp-for="FullName"

class="control-label

col-md-2"></label>

<div

class="col-md-10">

<input

asp-for="FullName"

class="form-control"

/>

<span

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

</div>A元素/upload/information/20210522/379/535362.pngForm元素/upload/information/20210522/379/535363.pngInput元素/upload/information/20210522/379/535364.png/upload/information/20210522/379/535366.png/upload/information/20210522/379/535367.pngLabel元素/upload/information/20210522/379/535368.pngtextarea元素/upload/information/20210522/379/535370.pngspan元素/upload/information/20210522/379/535371.pngdiv元素/upload/information/20210522/379/535373.pngselect元素/upload/information/20210522/379/535375.pnglink元素/upload/information/20210522/379/535378.png<link

rel="stylesheet"

href="http:///ajax/bootstrap-touch-carousel/0.8.0/css/bootstrap-touch-carousel.css"

asp-fallback-href="~/lib/bootstrap-touch-carousel/css/bootstrap-touch-carousel.css"

asp-fallback-test-class="carousel-caption"

asp-fallback-test-property="display"

asp-fallback-test-value="none"

/><link

rel="stylesheet"

href="http:///ajax/bootstrap-touch-carousel/0.8.0/css/bootstrap-touch-carousel.css"

/>

<meta

name="x-stylesheet-fallback-test"

class="carousel-caption"

/>

<script>

!function

(a,

b,

c)

{

var

d,

e

=

document,

f

=

e.getElementsByTagName("SCRIPT"),

g

=

f[f.length

-

1].previousElementSibling,

h

=

e.defaultView

&&

e.defaultView.getComputedStyle

?

e.defaultView.getComputedStyle(g)

:

g.currentStyle;

if

(h

&&

h[a]

!==

b)

{

for

(d

=

0;

d

<

c.length;

d++)

{

e.write('<link

rel="stylesheet"

href="'

+

c[d]

+

'"/>')

}

}

}("display",

"none",

["\/lib\/bootstrap-touch-carousel\/css\/bootstrap-touch-carousel.css"]);

</script>script元素/upload/information/20210522/379/535379.png<script

src="http:///ajax/jquery/jquery-1.10.2.min.js"

asp-fallback-src="~/lib/jquery/jquery.min.js"

asp-fallback-test="window.jQuery">

</script><script

src="http:///ajax/jquery/jquery-1.10.2.min.js">

</script>

<script>(window.jQuery||document.write("<script

src=\"\/lib\/jquery\/jquery.min.js\"><\/script>"));</script>Cache/upload/information/20210522/379/535380.png利用EnvironmentTagHelper來控制不同運(yùn)行環(huán)境的輸出結(jié)果<environment

names="Development">

<script

src="~/lib/jquery/jquery.js"></script>

<script

src="~/lib/bootstrap/js/bootstrap.js"></script>

<script

src="~/lib/hammer.js/hammer.js"></script>

<script

src="~/lib/bootstrap-touch-carousel/js/bootstrap-touch-carousel.js"></script>

</environment>

<environment

names="Staging,Production">

<script

src="http:///ajax/jquery/jquery-1.10.2.min.js"

asp-fallback-src="~/lib/jquery/jquery.min.js"

asp-fallback-test="window.jQuery">

</script>

<script

src="http:///ajax/bootstrap/3.0.0/bootstrap.min.js"

asp-fallback-src="~/lib/bootstrap/js/bootstrap.min.js"

asp-fallback-test="window.jQuery">

</script>

<script

src="http:///ajax/hammer.js/2.0.4/hammer.min.js"

asp-fallback-src="~/lib/hammer.js/hammer.js"

asp-fallback-test="window.Hammer">

</script>

<script

src="http:///ajax/bootstrap-touch-carousel/0.8.0/js/bootstrap-touch-carousel.js"

asp-fallback-src="~/lib/bootstrap-touch-carousel/js/bootstrap-touch-carousel.js"

asp-fallback-test="window.Zepto">

</script>

</environment>自定義TagHelperpublic

interface

ITagHelper

{

int

Order

{

get;

}

Task

ProcessAsync(TagHelperContext

context,

TagHelperOutput

output);

}public

interface

ITagHelper

{

int

Order

{

get;

}

Task

ProcessAsync(TagHelperContext

context,

TagHelperOutput

output);

}public

class

ATagHelper

:

TagHelper

{

[Activate]

public

IUrlHelper

UrlHelper

{

get;

set;

}

public

string

Controller

{

get;

set;

}

public

string

Action

{

get;

set;

}

public

override

void

Process(TagHelperContext

context,

TagHelperOutput

output)

{

if

(Controller

!=

null

&&

Action

!=

null)

{

var

methodParameters

=

output.Attributes.ToDictionary(attribute

=>

attribute.Key,

attribute

=>

(object)attribute.Value);

//

刪除所有的attributes,因?yàn)槁酚衫镆呀?jīng)可以自動(dòng)生成了

output.Attributes.Clear();

output.Attributes["href"]

=

UrlHelper.Action(Action,

Controller,

methodParameters);

output.PreContent.SetContent("My

");

}

}

}public

class

ATagHelper

:

TagHelper

{

[Activate]

public

IUrlHelper

UrlHelper

{

get;

set;

}

public

string

Controller

{

get;

set;

}

public

string

Action

{

get;

set;

}

public

override

void

Process(TagHelperContext

context,

TagHelperOutput

output)

{

if

(Controller

!=

null

&&

Action

!=

null)

{

var

methodParameters

=

output.Attributes.ToDictionary(attribute

=>

attribute.Key,

attribute

=>

(object)attribute.Value);

//

刪除所有的attributes,因?yàn)槁酚衫镆呀?jīng)可以自動(dòng)生成了

output.Attributes.Clear();

output.Attributes["href"]

=

UrlHelper.Action(Action,

Controller,

methodParameters);

output.PreContent.SetContent("My

");

}

}

}[TargetElement("p")]

public

class

AutoLinkerTagHelper

:

TagHelper

{

public

override

async

Task

ProcessAsync(TagHelperContext

context,

TagHelperOutput

output)

{

var

childContent

=

await

context.GetChildContentAsync();

//

Find

Urls

in

the

content

and

replace

them

with

their

anchor

tag

equivalent.

output.Content.SetContent(Regex.Replace(

childContent.GetContent(),

@"\b(?:https?://|www\.)(\S+)\b",

"$0"));

}

}[TargetElement("p")]

public

class

AutoLinkerTagHelper

:

TagHelper

{

public

override

async

Task

ProcessAsync(TagHelperContext

context,

TagHelperOutput

output)

{

var

childContent

=

await

context.GetChildContentAsync();

//

Find

Urls

in

the

content

and

replace

them

with

their

anchor

tag

equivalent.

output.Content.SetContent(Regex.Replace(

childContent.GetContent(),

@"\b(?:https?://|www\.)(\S+)\b",

"$0"));

}

@Model.CopyrightYear

-

My

ASP.NET

Application©

@Model.CopyrightYear

-

My

ASP.NET

Application[TargetElement("div")]

[TargetElement("style")]

[TargetElement("p")]

public

class

ConditionTagHelper

:

TagHelper

{

public

bool?

Condition

{

get;

set;

}

public

override

void

Process(TagHelperContext

context,

TagHelperOutput

output)

{

//

如果設(shè)置了condition,并且該值為false,則不渲染該元素

if

(Condition.HasValue

&&

!Condition.Value)

{

output.SuppressOutput();

}

}

}4.自定義元素的TagHelperpublic

class

WebsiteInformationTagHelper

:

TagHelper

{

public

WebsiteContext

Info

{

get;

set;

}

public

override

void

Process(TagHelperContext

context,

TagHelperOutput

output)

{

output.TagName

=

"section";

output.PostContent.SetContent(string.Format(

"

Version:

{0}

"

+

Environment.NewLine

+

"

Copyright

Year:

{1}

"

+

Environment.NewLine

+

"

Approved:

{2}

"

+

Environment.NewLine

+

"

Number

of

tags

to

show:

{3}

"

+

Environment.NewLine,

Info.Version.ToString(),

Info.CopyrightYear.ToString(),

Info.Approved.ToString(),

Info.TagsToShow.ToString()));

output.SelfClosing

=

false;

}

}public

class

WebsiteInformationTagHelper

:

TagHelper

{

public

WebsiteContext

Info

{

get;

set;

}

public

overr

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論