Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何编码&xE7öü;以安全的方式?_C#_Asp.net Mvc_Razor_Encoding_Special Characters - Fatal编程技术网

C# 如何编码&xE7öü;以安全的方式?

C# 如何编码&xE7öü;以安全的方式?,c#,asp.net-mvc,razor,encoding,special-characters,C#,Asp.net Mvc,Razor,Encoding,Special Characters,我正在尝试使用c#MVC Razor实现一个土耳其内容管理器 我知道我可以使用@Html.Raw(model.content)来获取非编码文本,但这也会造成一些安全问题、Xss、注入等 相反,如果我只是使用@model.content直接显示文本,我会得到以下html源代码,我认为这也会造成SEO问题 @model.content outputs : ......güvece dökün....... 原文是 @Html.Raw(model

我正在尝试使用c#MVC Razor实现一个土耳其内容管理器

我知道我可以使用
@Html.Raw(model.content)
来获取非编码文本,但这也会造成一些安全问题、Xss、注入等

相反,如果我只是使用
@model.content
直接显示文本,我会得到以下html源代码,我认为这也会造成SEO问题

@model.content outputs : ......güvece dökün.......
原文是

@Html.Raw(model.content) outputs : ......güvece dökün.......
如何避免mvc编码以下字符,同时保持html的安全性

ç,ü,ö


当然,我可以创建自己的html扩展,但我想知道是否有一种安全可靠的方法

正如我在问题中提到的,我使用html助手extesion将其分类

   @{
    string v = "<script>I Ğ Ü İ Ş Ç Ö ö ç i ş ü ğ ı ü ğ p ı o . ö ö ç ı ı n ü ğ ş a l e r t'\'\\'(x)</script>";
        @Html.SafeHtml(v);
        @Html.SafeHtmlV2(v);
    }

//code above outputs: &lt;script&gt;I Ğ Ü İ Ş Ç Ö ö ç i ş ü ğ ı ü ğ p ı o . ö ö ç ı ı n ü ğ ş a l e r t&#39;&#39;\&#39;(x)&lt;/script&gt

看起来像
模型。内容
有一个HTML实体,如
和#252而不是Unicode字符。检查页面的HTML源代码,可能您会看到
&#252;?如果是这样,请调用
System.Net.WebUtility.HtmlDecode
查看
model.content
public static class MyHelper
{
    public static MvcHtmlString SafeHtml(this HtmlHelper html, string input)
    {
        string[] decodeItems = new string[] { "&#252;", "&#246;", "&#231;", "&#220;", "&#199;", "&#214;" };
        string str = System.Net.WebUtility.HtmlEncode(input);
        foreach (string s in decodeItems)
        {
            str = str.Replace(s, System.Net.WebUtility.HtmlDecode(s));
        }
        return new MvcHtmlString(str);
    }

    public static MvcHtmlString SafeHtmlV2(this HtmlHelper html, string input)
    {
        string str = System.Net.WebUtility.HtmlEncode(input).Replace("&#252;", "ü")
               .Replace("&#246;", "ö")
               .Replace("&#231;", System.Net.WebUtility.HtmlDecode("&#231;"))
               .Replace("&#220;", System.Net.WebUtility.HtmlDecode("&#220;"))
               .Replace("&#199;", System.Net.WebUtility.HtmlDecode("&#199;"))
               .Replace("&#214;", System.Net.WebUtility.HtmlDecode("&#214;"));
        return new MvcHtmlString(str);
    }

}