




版權(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 電影票務(wù)平臺(tái)地區(qū)級代理合同
- 合同法修訂案:第一章 合同的訂立與生效
- 外資制造業(yè)-員工培訓(xùn)合同范本
- 木材采購與銷售合同模板
- 流動(dòng)人口計(jì)劃生育協(xié)作合同
- 干股收益分配合同(范本)
- 企事業(yè)單位監(jiān)控布防合同模板
- 合同責(zé)任死亡賠償金額解析
- 學(xué)校食堂食材采購合同模板
- 陳獨(dú)秀生平事跡
- 2024年人教版初三數(shù)學(xué)(下冊)模擬試卷及答案(各版本)
- 《大學(xué)美育》高職全套教學(xué)課件
- 醫(yī)院CT機(jī)房裝飾改造工程施工組織設(shè)計(jì)
- DZ∕T 0213-2020 礦產(chǎn)地質(zhì)勘查規(guī)范 石灰?guī)r、水泥配料類(正式版)
- 《行政倫理學(xué)教程(第四版)》課件 張康之 第8-13章 行政組織倫理-技術(shù)時(shí)代的行政倫理
- 《水電廠應(yīng)急預(yù)案編制導(dǎo)則》
- MOOC 跨文化交際通識(shí)通論-揚(yáng)州大學(xué) 中國大學(xué)慕課答案
- GB/T 2471-2024電阻器和電容器優(yōu)先數(shù)系
- 成人住院患者靜脈血栓栓塞癥預(yù)防護(hù)理
- 三年級道德與法治3.我很誠實(shí) 說課一等獎(jiǎng)
評論
0/150
提交評論