using System.Web;public static string HtmlEncode(string Htmlstr)
    {
        string Htmlcode = HttpContext.Current.Server.HtmlEncode(Htmlstr);
        return Htmlcode;
    }搞不懂为什么要报这个错。特来请教!如何解决呢??

解决方案 »

  1.   

    看你的Htmlstr里有值没有。
    好像是没有值传过来的原因
    打个断点调试一下。
      

  2.   

    string Htmlcode = HttpContext.Current.Server.HtmlEncode("aaaa").ToString();
      

  3.   

    string Htmlcode = HttpContext.Current.Server.HtmlEncode(Htmlstr).ToString();
      

  4.   

    return HttpContext.Current.Server.HtmlEncode(string.IsNullOrEmpty(Htmlstr)?"":Htmlstr);
      

  5.   

    HttpContext.Current只是在ASP.NET中处理用户发出的请求时才有效参考:http://www.west-wind.com/presentations/dotnetWebRequest/dotnetWebRequest.htm
      

  6.   

    我的目的其实很简单
    就是在App_code的.cs中实现Server.HtmlEncode这种编码的效果,有其它办法实现吗?
      

  7.   

    我的目的其实很简单
    就是在App_code的.cs中实现字符编码,象Server.HtmlEncode这种编码的效果,有其它办法实现吗?
      

  8.   

    App_code里的.cs经常出些莫名的问题,把.cs放到别的文件夹下就好了吧
      

  9.   

    我必须要在App_code下.cs中对字符编码,如何才能办到哦?
      

  10.   

    App_code中是可以使用HttpContext.Current的

    using System.Web;
    public void Alert(string message)
        {
            string js = @"<Script language='JavaScript'>
                        alert('" + message + "');</Script>";
            HttpContext.Current.Response.Write(js);
        }
      

  11.   

    System.Web.HttpUtility.HtmlEncode()  这个方法试试
      

  12.   


    感谢帮我指了条路,改成这样就对了
    System.Web.HttpUtility.UrlEncode();