我有一个表单,用户可以在其中输入 YouTube 视频的网址。我的代码解析 url 并创建一个存储在我的数据库中的嵌入字符串。这看起来像:
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>";
该字符串存储在数据库中,稍后检索并打印到 HTML 页面。但是,当我查看页面源代码时,输出最终看起来像这样:
<iframe width="640" height="360" src="http://www.youtube.com/embed/AXaoi6dz59A" frameborder="0" allowfullscreen></iframe>
如何打印此字符串,使其不会转义引号和小于/大于符号?
最佳答案
我认为你应该使用@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/
关于C# ASP.net,使用字符串变量存储和编写 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10137206/