0

I'm passing a string with a URL from the controller to the view (using a model). While the string is in the controller, it is not encoded, but in the view the URL is encoded.

URL before encoding:

http://app.xpinator.com/FacebookPayments/FacebookDesktopAdData?paymentCode=s5usd920k&userLocale=en-us&ver=v3.0.1

URL after encoding:

http://localhost/FacebookPayments/FacebookPaymentItemData?paymentcode=v250usd45000k&userlocale=en-us&ver=v3.0.1

Relevant line in the view:

<meta property="og:url" content="@Model.URL" />

I want to display a decoded URL in the view. I tried using HttpUtility.HtmlDecode, HttpUtility.UrlDecode and Html.Raw - nothing worked.

Any ideas?

EDIT:

Thanks to Daniel's comment I realized that the encoding is happening only when the URL is in a meta property. when its a "displayable" HTML there is no encoding.

Anyway, still looking for a solution.

goldor
  • 31
  • 2
  • 6

1 Answers1

0

After realizing the problem occurs only with meta tags, I've found this thread: Why is Html.Raw escaping ampersand in anchor tag in ASP.NET MVC 4?

I managed solve the problem with the suggested workaround to make the entire meta tag part of the raw value.

like this:

Controller:

model.URL = string.Format("{0}{1}{2}", "<meta property=\"og:url\" content=\"", paymentItemOG.URL, "\" />");

View:

<meta property="og:image" content="@Model.Image" />
@Html.Raw(Model.URL)
<meta property="og:description" content="@Model.Description" />

Page view-source:

<meta property="og:image" content="http://localhost/content/images/75x75_fullLogo.png" />
<meta property="og:url" content="http://localhost/FacebookPayments/FacebookPaymentItemData?paymentcode=v250usd45000k&userlocale=en-us&ver=v3.0.1" />
<meta property="og:description" content="texttexttext" />

Now it works great.

Community
  • 1
  • 1
goldor
  • 31
  • 2
  • 6