I applied the method to this:
@helper RenderConnectButton(System.Web.Mvc.HtmlHelper helper, ContactTwitterHandleDto contactTwitterHandle, ContactFacebookAccountDto contactFacebookAccount) { <ul class="socialMedia inlineList"> @if (contactTwitterHandle != null && contactTwitterHandle.IsValid.HasValue && contactTwitterHandle.IsValid.Value) { var tHandle = contactTwitterHandle.Handle; <li class="twitter"> <a class="btn" href="http://twitter.com/@tHandle" target="_blank"> <i></i> <span class="label">@tHandle</span> </a> </li> } </ul> }
If I remove @tHandle from the line:
<a class="btn" href="http://twitter.com/@tHandle" target="_blank">
He will understand perfectly. If I put Url above in a variable, I will get an error.
Unable to parse a string that is Url.
Stack trace:
at System.Web.WebPages.HelperPage.WriteAttributeTo(TextWriter writer, String name, PositionTagged`1 prefix, PositionTagged`1 suffix, AttributeValue[] values) at ASP.MvcSocialMediaHelpers.<>c__DisplayClass1.<RenderConnectButton>b__0(TextWriter __razor_helper_writer) in c:\OberonScrum\Branches\RazorUpgrade\Source\Web\App_Code\MvcSocialMediaHelpers.cshtml:line 83 at System.Web.WebPages.HelperResult.ToString() at System.String.Concat(Object arg0, Object arg1) at ASP.views_contact_detail_aspx.__RenderMainContent(HtmlTextWriter __w, Control parameterContainer) in c:\OberonScrum\Branches\RazorUpgrade\Source\Web\Views\Contact\Detail.aspx:line 13 at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) at ASP.views_shared_twocolumnleftmain_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\OberonScrum\Branches\RazorUpgrade\Source\Web\Views\Shared\TwoColumnLeftMain.Master:line 109 at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) at System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
[UPDATE] Tried to work with the problem using TagBuilder and rendering using Html.Raw (); got the same result.
@helper RenderConnectButton(System.Web.Mvc.HtmlHelper helper, ContactTwitterHandleDto contactTwitterHandle, ContactFacebookAccountDto contactFacebookAccount) { <ul class="socialMedia inlineList"> @if (contactTwitterHandle != null && contactTwitterHandle.IsValid.HasValue && contactTwitterHandle.IsValid.Value) { var tHandle = contactTwitterHandle.Handle; var a = new TagBuilder("a"); a.Attributes.Add("href", "http://twitter.com/" + @tHandle); a.Attributes.Add("target", "_blank"); a.Attributes.Add("class", "btn"); a.InnerHtml = string.Format("<i></i><span class=\"label\">{0}</span>", @tHandle); <li class="twitter"> @Html.Raw(a.ToString()) </li> } </ul> }