html.Encode(os).Replace("\r\n", "
").Replace(" ", " ");

wrost 2013-05-06 10:36:33



Replace("\r\n", "<br/>").Replace(" ", " "); 这一段我知道,表相应的空格、换行,转换成 html的表现形式

问题:
html.Encode(os)这个我就不太懂了,个进行跟踪,Encode(os)前后,好像字符串没有发什么变化



public static string FormatHtml(this string os,HtmlHelper html)
{
return html.Encode(os).Replace("\r\n", "<br/>").Replace(" ", " ");
}
...全文
329 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
newtee 2013-05-06
  • 打赏
  • 举报
回复
没变化=没编码
newtee 2013-05-06
  • 打赏
  • 举报
回复
编码啊 没变化说明没有需要编码的字符 比如 < 编后变成 <
newtee 2013-05-06
  • 打赏
  • 举报
回复
怎么弄mono上了啊 不过MVC 1.0也这么写的
newtee 2013-05-06
  • 打赏
  • 举报
回复
HtmlHelper.HtmlEncode
   [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic",
            Justification = "For consistency, all helpers are instance methods.")]
        public string Encode(string value) {
            return (!String.IsNullOrEmpty(value)) ? HttpUtility.HtmlEncode(value) : String.Empty;
        }
 
newtee 2013-05-06
  • 打赏
  • 举报
回复
难道代码不是你写的 你这扩展方法写的有点看不懂啊 看源码 不解释 http://www.oschina.net/code/explore/mono-2.8.1/mcs/class/System.Web.Mvc/System.Web.Mvc/HtmlHelper.cs
public static unsafe void HtmlEncode(string value, TextWriter output)
{
    if (value != null)
    {
        if (output == null)
        {
            throw new ArgumentNullException("output");
        }
        int num = IndexOfHtmlEncodingChars(value, 0);
        if (num == -1)
        {
            output.Write(value);
        }
        else
        {
            int num2 = value.Length - num;
            fixed (char* str = ((char*) value))
            {
                char* chPtr = str;
                char* chPtr2 = chPtr;
                while (num-- > 0)
                {
                    output.Write(chPtr2[0]);
                    chPtr2++;
                }
                while (num2-- > 0)
                {
                    char ch = chPtr2[0];
                    if (ch <= '>')
                    {
                        switch (ch)
                        {
                            case '&':
                            {
                                output.Write("&");
                                chPtr2++;
                                continue;
                            }
                            case '\'':
                            {
                                output.Write("'");
                                chPtr2++;
                                continue;
                            }
                            case '"':
                            {
                                output.Write(""");
                                chPtr2++;
                                continue;
                            }
                            case '<':
                            {
                                output.Write("<");
                                chPtr2++;
                                continue;
                            }
                            case '>':
                            {
                                output.Write(">");
                                chPtr2++;
                                continue;
                            }
                        }
                        output.Write(ch);
                        chPtr2++;
                        continue;
                    }
                    // !here is the point!
                    if ((ch >= '\x00a0') && (ch < 'Ā'))
                    {
                        output.Write("&#");
                        output.Write(ch.ToString(NumberFormatInfo.InvariantInfo));
                        output.Write(';');
                    }
                    else
                    {
                        output.Write(ch);
                    }
                    chPtr2++;
                }
            }
        }
    }
}
wrost 2013-05-06
  • 打赏
  • 举报
回复
引用 1 楼 zhuankeshumo 的回复:
编码啊 没变化说明没有需要编码的字符 比如 < 编后变成 <
谢谢 既然已经进行编码,为什么还要对空格、换行单独进行编码?

62,039

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧