I looked thoroughly for an answer for this specific problem but cannot seem to find an answer.
I have the below TagBuilder that takes a string url created by a UrlHelper. The issue is that once I use MergeAttribute for the href attribute, the & gets converted to &.
public static IHtmlString ModalLink(this HtmlHelper helper, string linkText, string link, string title = "External Link", string actionName="External", string controllerName="ModalPopup")
{
var anchor = new TagBuilder("a");
UrlHelper context = new UrlHelper(HttpContext.Current.Request.RequestContext);
string urlString = context.Action(
actionName,
controllerName,
new {Title = title, Link = link}
);
anchor.MergeAttribute("href", urlString);
anchor.InnerHtml = linkText;
return MvcHtmlString.Create(anchor.ToString());
}
I verified that urlString is giving me the url I expect, but once it is added to the TagBuilder through MergeAttribute it is being html encoded. In my view I tried both
@Html.Raw(Html.ModalLink("blah blah", "http://www.example.com", "Example"))
and
@Html.ModalLink("blah blah", "http://www.example.com", "Example")
but both return the same ampersand in the returned value. I am obviously new to ASP.NET, but it's important to me that I create a clean and elegant method and I would prefer not to use Html.Raw.
Is there a way to keep the & from formatting to & , or is there a better way to solve my issue?