2

I have a form where users can enter a url to a YouTube video. My code parses the url and creates an embed string that is stored in my database. This looks something like:

string rawQuery = uri.Query;
int index = rawQuery.IndexOf("?");
if (index > 0)
                rawQuery = rawQuery.Substring(index).Remove(0, 1);
id = HttpUtility.ParseQueryString(rawQuery).Get("v");

string embedURL = "<iframe width=\"640\" height=\"360\" src=\"http://www.youtube.com/embed/" + id + "\" frameborder=\"0\" allowfullscreen></iframe>";

This string gets stored in the database and retrieved later and printed to an HTML page. However, the output ends up looking like this when I view the page source:

&lt;iframe width=&quot;640&quot; height=&quot;360&quot; src=&quot;http://www.youtube.com/embed/AXaoi6dz59A&quot; frameborder=&quot;0&quot; allowfullscreen&gt;&lt;/iframe&gt;

How can I print this string so that it doesn't escape my quotes and less/greater than symbols?

0

3 Answers 3

2

I think you should use @Html.Raw(string)

http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx

http://www.arrangeactassert.com/using-html-raw-in-asp-net-mvc-razor-views/

Sign up to request clarification or add additional context in comments.

Comments

1

If you use, for example, ViewBag to output the string in the view use

@Html.Raw(ViewBag.EmbedURL)

This tell the framework to not encode the string

Comments

0

It's when you write the string in the page that it gets encoded. Use the <%= %> server tag to output the string without encoding it:

<%= embedURL %>

You can also wrap it in an HtmlString object to avoid the automatic encoding:

HtmlString html = new HtmlString(embedURL)

Then you can just output it as normal:

<%: html %>

or using razor:

@html

You can also use the Html.Raw method to wrap it in an HtmlString object:

@Html.Raw(embedURL)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.