'Displaying an iframe with @Html.Raw is not working like when displaying Script with @Html.Raw

Note: This is written in asp.net mvc 5. That is the tech I am using for this.

I am used to displaying certain data when I use @Html.Raw to show data from a Model on the front end of my site for customers. The following example below always works for me with no issues.

@Html.Raw(Model.MyContentToShowWith_Script)

//FYI: Model.MyContentToShowWith_Script has the following value of a script in my database:
//<script src="https://apps.exampleThirdPartyService.com/p/100201/platform.js" defer></script>

//This returns the third party's data that I need to be displayed.

My current issue is that when I want to use iframe in stead of script, nothing happens, I just see a bunch of raw javascript on the page and NOT the same data that I was seeing when I was not using iframe. Does @Html.Raw not work with iframe? Below is the code that I am trying to make work. By "work" I mean I need it to show exactly what shows up when I use script.

@Html.Raw(Model.MyContentToShowWith_iFrame)

//FYI: Model.MyContentToShowWith_iFrame has the following value of an iframe in my database:
//<iframe src="https://apps.exampleThirdPartyService.com/p/100201/platform.js" defer></iframe>

// This SHOULD return the same data as the working example above but just returns the raw js.

If for example I were to statically write the iframe on the page to replace the dynamic Html.Raw call, then the iframe content does work. BUT not when its coming from the Html.Raw. (Of course me adding it statically to the site was just a test to prove that the iframe is indeed working. I would however be calling this dynamically in production).

So as you can see from my 2 scenarios above the only thing that is changing is that I went from using Script to iframe. The result is that iframe does not return the data for me the same way that using script would show it.

How can I fix this? What is actually going on in my situation that does not allow iframe to display and makes it only show the raw javascript?



Solution 1:[1]

Check the below to fix your issue,

  • We can use HttpUtility.HtmlDecode

  • Similar kind of issue fixed here.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TechieWords