HttpContext.Current为什么经常找不到??

Lukiya 2005-11-15 01:30:36
HttpContext.Current这个类经常出现未将对象实例化的错误。
...全文
768 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
baffling 2006-02-21
  • 打赏
  • 举报
回复
gz
Lukiya 2005-11-18
  • 打赏
  • 举报
回复
楼上这个解释能理解一些。
那该怎么办呢。有时候明明是人在请求但莫名奇妙会变成不是人的请求……
pontus 2005-11-17
  • 打赏
  • 举报
回复
HttpContext.Current 只是在响应用户请求的时候才会返回正确的实例,否则返回为空
clxxj 2005-11-17
  • 打赏
  • 举报
回复
一直都在用未发生过此问题.

是不是HTTP请求的哪个对象本身就不存在?
guoguo19811025 2005-11-17
  • 打赏
  • 举报
回复
出现这个问题应该不是current属性的的问题了,比如说server.mappath()你的路径对了没有.

个人意见,仅供参考.

一般情况下,我总是遇到找不到current,一般都是缺少引用
Lukiya 2005-11-17
  • 打赏
  • 举报
回复
看见没,果然不只我遇到过。

有没有高手能说明下并给个解决方案呢??
ntcw 2005-11-16
  • 打赏
  • 举报
回复
ding
ntcw 2005-11-16
  • 打赏
  • 举报
回复
奇怪, 我也碰到, 有时初始化时HttpContext.Currect.Server.MapPath()正常, 再调用时就出错.
到现在都不明白, HttpContext.Currect何时为NULL呢?

我是用Time调用类的。
Lukiya 2005-11-15
  • 打赏
  • 举报
回复
晕,不是这个意思。我是说调用他的方法经常发生 未将对象实例化 的错误

例如HttpContext.Currect.Server.MapPath()
例如HttpContext.Currect.Cache[...]

等等。
rockplayer 2005-11-15
  • 打赏
  • 举报
回复
不用实例化的,如果上面的名字空间没引用的话就写全一些好了.System.Web.HttpContext.Current
zx810903 2005-11-15
  • 打赏
  • 举报
回复
HttpContext context = HttpContext.Currect;
jxufewbt 2005-11-15
  • 打赏
  • 举报
回复
Current不是类,它只是HttpContext类的一个属性而已。
lincai 2005-11-15
  • 打赏
  • 举报
回复
System.Web.HttpContext.Current
场景:在自定义控件、用户控件、页面、后台代码都会有引用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");   } }
转自:http://www.yongfa365.com/item/ASP.net-Forms-Demo.html 如果您研究过这个问题,那么,你一定会比较郁闷,现在网上流行的那个,国产的,里面有点问题,反正我一从昨天研究到现在 2008年12月17日 23时59分10秒,才搞明白是怎么回事,你说我们这些人容易吗!如果只是为了完成任务,我用ASP就OK了,为什么还要用ASP.net,如果我们用.net时还用ASP的思路也得了,为什么还要研究ASP.net提供的东西。呵呵,不为什么,喜欢,我所做的正是我想做的,我不是为了完成一个任务,而是为了提升自己。总之一句话:我的目标是简化生活,技术高了,就不怕出错了,天塌下来也能给他顶回去。 言归正传,首先,贴上人家外国人的地址:http://www.codeproject.com/KB/web-security/formsroleauth.aspx 有兴趣的可以看看,没兴趣的直接下我的Demo,有点兴趣的可以看下国人处理时有问题的地方: Global.asax protected void Application_AuthenticateRequest(Object sender, EventArgs e) { if (HttpContext.Current.User != null)//如果当前的http信息中存在用户信息 { if (HttpContext.Current.User.Identity.IsAuthenticated)//如果当前用户的身份已经通过了验证 { if (HttpContext.Current.User.Identity is FormsIdentity) { //如果当前用户身份是FormsIdentity类即窗体验证类,此类有个属性能够访问当前用户的验证票 FormsIdentity fi = (FormsIdentity)HttpContext.Current.User.Identity;//创建个FormsIdentity类,用他来访问当前用户的验证票 //获得用户的验证票 FormsAuthenticationTicket ticket = fi.Ticket; //从验证票中获得用户数据也就是角色数据 string userData = ticket.UserData; //把用户数据用,分解成角色数组 string[] roles = userData.Split(','); //重写当前用户信息,就是把角色信息也加入到用户信息中 HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(fi, roles); } } } } 最后,贴上国人的地址,其实文章写的不错,只是这个事件没写对,其它的都不错,地址是:Asp.net中基于Forms验证的角色验证授权

62,046

社区成员

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

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

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

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