在一个自定义的类中怎么引用System.Web.HttpContext?

wflaugh 2005-07-29 10:12:50
我在一个自定义的类中,有一个连接数据库的字符串,需要这样写System.Web.HttpContext.Current.Server.MapPath("Data/Data.mdb")
但,却提示HttpContext在System.Web中不存在,是否缺少引用 ,但,直接在网页的cs后边就没有问题
...全文
904 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
wflaugh 2005-07-29
  • 打赏
  • 举报
回复
还有没有好的解决办法?
wflaugh 2005-07-29
  • 打赏
  • 举报
回复
是的,数据库层是一个单独的项目,我是根据petshop修改了一下,想在里面弄一个数据库连接的代码,我已经引用了using System.Web;
System.Web.HttpContext.Current.Server.MapPath("Data/Data.mdb");还是提示HttpContext没有被引用
FranckyTang 2005-07-29
  • 打赏
  • 举报
回复
不用楼上说的这么复杂
据我分析你所说的数据库访问层应该是一个类库而且是一个单独的项目
你只要在这个项目中添加对System.Web的引用就行了
netpot 2005-07-29
  • 打赏
  • 举报
回复
实际上这个类是systemframeWork处理的
1、在sysframeWork类中添加对system.web的引用
2、定义静态变量,如下
public static System.Web.HttpContext httpContext;
3、在global.asax的Application_Start初始化httpContext
然后在数据访问层直接引用sysframeWork.httpContext.Current等就可以了
wflaugh 2005-07-29
  • 打赏
  • 举报
回复
楼上的跟我写的代码有什么不同吗?在数据库层中,我只能引用到system.web,后边的就都不能引用了,不知道是怎么回事
wufeng0524 2005-07-29
  • 打赏
  • 举报
回复
public string v()
{
return System.Web.HttpContext.Current.Server.MapPath("Data/Data.mdb");
}
是可以的。。。

__________________________________________
DotNet中华网:www.aspxcn.org
xzq686 2005-07-29
  • 打赏
  • 举报
回复
加上Current试试下面的:
System.Web.HttpContext.Current.Server.MapPath("Data/Data.mdb")
wflaugh 2005-07-29
  • 打赏
  • 举报
回复
因为我有一个数据库访问的层,其中,我想把数据库的连接字符传放在这曾中,要得到数据库的相对地址,只能使用System.Web.HttpContext.Current.Server.MapPath("Data/Data.mdb")
,但,在数据库访问层中,根本没有办法访问System.Web.HttpContext,,不知道大家是怎么解决的~
wflaugh 2005-07-29
  • 打赏
  • 举报
回复
现在特别急
xiaowangtian1117 2005-07-29
  • 打赏
  • 举报
回复
要想得到一个物理路径可以用Page.MapPath("Data/Data.mdb")
OSCAR_NJU 2005-07-29
  • 打赏
  • 举报
回复
请确定你的项目引用了system.web.dll,不是web项目默认是不会引用的吧
ssuunnbbiirrdd 2005-07-29
  • 打赏
  • 举报
回复
添加using system.web就行了
建哥聊IT 2005-07-29
  • 打赏
  • 举报
回复
添加 using System;
场景:在自定义控件、用户控件、页面、后台代码都会有引用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,046

社区成员

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

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

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

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