Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

asp.net mvc - How do I bypass the HTML encoding when using Html.ActionLink in Mvc?

Whenever I use Html.ActionLink it always Html encodes my display string. For instance I want my link to look like this:

<a href="/posts/422/My-Post-Title-Here">More&hellip;</a>

it outputs like this: More&hellip;

&hellip is "..." incase you were wondering.

However the actionlink outputs the actual text "&hellip;" as the link text. I have the same problem with if I want to output this:

<a href="/posts/422/My-Post-Title-Here"><em>My-Post-Title-Here</em></a>

I wind up with: <em>My-Post-Title-Here</em>

Any idea how to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It looks like ActionLink always uses calls HttpUtility.Encode on the link text. You could use UrlHelper to generate the href and build the anchor tag yourself.

<a href='@Url.Action("Posts", ...)'>More&hellip;</a>

Alternatively you can "decode" the string you pass to ActionLink. Constructing the link in HTML seems to be slightly more readable (to me) - especially in Razor. Below is the equivalent for comparison.

@Html.ActionLink(HttpUtility.HtmlDecode("More&hellip;"), "Posts", ...)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...