19212

How to escape html.actionLink tags in string.format

Question:

To make an URL within a translatable text we use {0} as placeholder.

So we do:

@string.Format(@translationHelper.GetTranslation("label-ClickToContinue"), @Html.ActionLink( @translationHelper.GetTranslation("text-here"), "Login", new { Model.UserName, Model.UniqueId } ) )

Translation keys:

label-ClickToContinue = "Click {0} to continue" text-here = "here"

But this prints the escaped string into the source: <a href="/Login/Login?UserName=alberttest3&UniqueId=f3647fed-bab4-4575-bb5f-98ed27edff43">label-RequestNewOfficeWizard</a>

How to make sure that it'll not show the html-tag, but the URL?

Answer1:

It easy <strong>just put the output from string.Format() inside a Html.Raw()</strong> like this:

@Html.Raw(string.Format(@translationHelper.GetTranslation("label-ClickToContinue"), @Html.ActionLink( @translationHelper.GetTranslation("text-here"), "Login", new { Model.UserName, Model.UniqueId } ) ))

Translation keys:

label-ClickToContinue = "Click {0} to continue" text-here = "here"

Answer2:

This is not supported by the ActionLink helper. It always HTML encodes the text. You could either write your own custom helper that doesn't encode the text or simply:

<a href="@Url.Action(Login, new { UserName, Model.UniqueId })"> @Html.Raw(translationHelper.GetTranslation("text-here")) </a>

By the way now that I have posted this code it looks so ugly. Go ahead and write a custom helper:

public static class HtmlExtensions { public static IHtmlString ActionLinkLocalized( this HtmlHelper html, string translationText, string actionName, object routeValues ) { var urlHelper = new UrlHelper(html.ViewContext.RequestContext); var anchor = new TagBuilder("a"); anchor.Attributes["href"] = urlHelper.Action(actionName, routeValues); anchor.InnerHtml = TranslationHelper.GetTranslation(translationText); return new HtmlString(anchor.ToString()); } }

and in your view simply:

@Html.ActionLinkLocalized("text-here", "Login", new { UserName, Model.UniqueId })

Recommend

  • Save selected text (partial line) from Vim
  • Classic ASP (VBScript) convert HTML codes to plain text
  • How to add the value for each label to pie legend
  • JQuery UI Datepicker Disbale Next Day After 12pm
  • How to do handle login-logoff
  • How to use multi dimensional STL vector in MPI
  • Quartz clustering load balancing algorithm internal implementation in Jdbcjobstore
  • Hadoop MapReduce sort reduce output using the key
  • How to access GWT's JsInterop exported types from browser console?
  • php7 session_start() times out page
  • Jenkins Composite baseline with ClearCase
  • IE6 postback in asp.net
  • Docker build error: “could not connect to server” (behind proxy)
  • Random number of FORM fields being prepared for database
  • performance counter events associated with false sharing
  • Sharing multiple images on facebook post
  • Create One-To-One relationship between table and view in EF4?
  • App crashes when switching to landscape layout
  • Is there a way to run c# forms application without showing a gui window (like a console application)
  • Python Asyncio run_forever() and Tasks
  • Google Apps Script fails to generate image from EmbeddedChartBuilder
  • Questions related to Garbage Collector and finalize() method
  • Was there ever a proposal to include the URL fragment into the HTTP request?
  • How to display youtube video on HTML page? [closed]
  • Excel Reverse Match
  • Create .java file and compile it to a .class file at runtime
  • Getting Microsoft Calibri font on Amazon EC2 ubuntu
  • Pandas time series data Index from a string to float [duplicate]
  • How to make Rss News Reader application in android …? [closed]
  • SQL Server 2012 not showing unicode character in results
  • Year over Year Stats from a Crossfilter Dataset
  • Tensorflow Dataset API restore Iterator after completing one epoch
  • Ruby regex for matching simpliest Ruby's regexes
  • Why is ordered choice in pyparsing failing for my use case?
  • Change cell value based on cell color in google spreadsheet
  • Google App Engine Datastore: Dealing with eventual consistency
  • How to use FirstOrDefault inside Include
  • PHP Permalinks.. how to change?
  • Running R's aov() mixed effects model from Python using rpy2
  • ReferenceError: TextEncoder is not defined