关于httpcontext~~~~~~~~~~~~~~~~~~~~

hottown1981 2006-08-14 11:58:53
1.httpcontext.current.session["aaa"]=5;
与Session["aaa"]=5;
2.httpcontext.current.response.redirect("********");
与response.redirect("******");

以上2组里面的2个写法是否等价??
httpcontext.current 里面的session ,application,request,response.......和不加httpcontext.current 有什么不同?

其实我就是不明白httpcontext是什么东东???
我感觉把httpcontext.current去了以后效果一样呢
...全文
172 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
hottown1981 2006-08-14
  • 打赏
  • 举报
回复
哦~~~原来是在类里用的啊
我再思考一下,谢谢
建哥聊IT 2006-08-14
  • 打赏
  • 举报
回复
httpcontext指出当前进行会话的内容.
你在class中不使用这个东东是不行的.
场景:在自定义控件、用户控件、页面、后台代码都会有引用JS的可能,这就会出现混乱或者重复引用的可能。 一个自定义控件,用于在ASPX页面中注册JS: public class Script : Control {   #region 属性   private string m_Src;   ///    /// 脚本文件路径   ///    public string Src   {     get { return m_Src; }     set { m_Src = value; }   }   #endregion   ///    /// 在控件Init的时候将JS路径添加到HttpContext.Current.Items["IncludedJavaScript"]中。   ///    ///    protected override void OnInit(EventArgs e)   {     base.OnInit(e);     if (!string.IsNullOrEmpty(Src))     {       string src = ResolveUrl(Src);       List includedJs = HttpContext.Current.Items["IncludedJavaScript"] as List;       if (null == includedJs)       {         includedJs = new List();         HttpContext.Current.Items["IncludedJavaScript"] = includedJs;       }       if (!includedJs.Contains(src))       {         includedJs.Add(src);       }     }   } } 一个静态类,用于管理JS和在后台代码(cs文件)中注册JS: ///  /// Javascript管理器 ///  public static class JavaScriptManager {   ///    /// 包含JS引用。   ///    ///    public static void Include(params string[] filePaths)   {     HttpContext context = HttpContext.Current;     if (null == context)     {       throw new Exception("HttpContext为空。");     }     System.Web.UI.Page p = context.CurrentHandler as System.Web.UI.Page;     if (null == p)     {       throw new Exception("HttpContext.CurrentHandler不是Page。");     }     IList jss = GetIncludedJavaScript();     string resolveUrl;     foreach (string filePath in filePaths)     {       resolveUrl=p.ResolveUrl(filePath);       if (!jss.Contains(resolveUrl))       {         jss.Add(p.ResolveUrl(resolveUrl));       }     }   }   ///    /// 获取已经包含的JS列表   ///    ///    public static IList GetIncludedJavaScript()   {     HttpContext context = HttpContext.Current;     if (null == context)     {       throw new Exception("HttpContext为空。");     }     IList jss = HttpContext.Current.Items["IncludedJavaScript"] as IList;     if (null == jss)     {       jss = new List();       HttpContext.Current.Items["IncludedJavaScript"] = jss;     }     return jss;   } } 然后写一个基类页面,所有的页面都要继承自这个基类页: public class BasePage : System.Web.UI.Page {   public BasePage() { }   #region 注册/管理JS引用   ///    /// 将引用的JS添加到Page.Head中。   ///    private void InitJS()   {     IList includedJs = JavaScriptManager.JavaScriptManager.GetIncludedJavaScript();     foreach (string jsFilePath in includedJs)     {       var script = new HtmlGenericControl("script");       script.Attributes["type"] = "text/javascript";       script.Attributes["src"] = jsFilePath;       Page.Header.Controls.Add(script);     }   }   ///    /// 在呈现之前注册JS   ///    ///    protected override void OnPreRender(EventArgs e)   {     base.OnPreRender(e);     InitJS();   }   #endregion } 上面是在OnPreRender中将JS注册到Page.Head中的,所以如果在自定义控件中注册JS引用,请在OnPreRender之前引用。 在ASPX页面中注册JS:                                  在CS页面中注册JS: public partial class _Default : BasePage {   protected void Page_Load(object sender, EventArgs e)   {     JavaScriptManager.JavaScriptManager.Include("~/JS/cs.js",       "~/JS/cs.js",       "~/JS/cs.js2",       "~/JS/cs.js");   } }

62,243

社区成员

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

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

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

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