显示特殊字符

时间:2013-05-25 18:23:06

标签: asp.net asp.net-mvc-4 razor

@{
    Layout = null;
    string root = Server.MapPath("~");
    string path = @"Content\galleries\galleries\";
    DirectoryInfo Dir = new DirectoryInfo(root + path + (string)ViewData["Name"]);
    FileInfo[] FileList = Dir.GetFiles("*.jpg");
    string response = "";
}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <script type="text/javascript">
        jQuery(function ($) {

            var array = new Array();
            var galleryname = '@ViewData["Name"]';

            @for (int i = 0; i < FileList.Length; i++)
            {
                string s = @"
                var path = '../Content/galleries/galleries/" + @ViewData["Name"] + @"/" + @FileList[i].Name + @"';
                var thumbpath = '../Content/galleries/galleries/" + @ViewData["Name"] + @"/thumbs/" + @FileList[i].Name + @"';
                array[" + i.ToString() + @"] =
                    {
                        image: path,
                        title: '" + @ViewData["Name"] + @"',
                        thumb: thumbpath,
                        url: path
                    };";
                response += s;
            }
            @response

这是我的代码。我想用asp.net动态编写一些javascript代码。当我用@response打印字符串'response'时,在页面源中我可以看到所有'字符都被更改为&amp; #39; 。那么所有路径都不好。我也尝试在循环中使用Response.Write,并且'字符不会更改,但是生成的代码放在页面源的最开头,在doctype之前,当然,没有任何作用。

如何解决这些问题?或者至少其中一个;)?或者还有另一种方式吗?

1 个答案:

答案 0 :(得分:2)

指示输出原始文本:

@Html.Raw(response)