ASP.NET数据访问结构类

CNBLOCK 2006-08-21 09:57:49
类的实现如下,执行到string assemblyFilePath =page.Request.ApplicationPath的时候出现错误提示System.Web.HttpException: 请求在此上下文中不可用,请问如何解决。
using System;
using System.IO;
using System.Reflection;
using System.Web;
using System.Web.UI.WebControls;
namespace DAL
{

public class ConnectionString:System.Web.UI.Page
{
/// <summary>
/// 获取数据链接地址
/// </summary>
/// <returns></returns>
public static string GetConnection()
{
System.Web.UI.Page page=new System.Web.UI.Page();
string assemblyFilePath =page.Request.ApplicationPath;
string configFilePath = page.MapPath(assemblyFilePath).Replace("/","\\") + "\\AppConfig.config";
// string assemblyFilePath = Assembly.GetExecutingAssembly().Location;
// string assemblyDirPath = Path.GetDirectoryName(assemblyFilePath);
// string configFilePath = assemblyDirPath + "\\AppConfig.config";
Sailun.SqlServerDal.Config config = new Config(configFilePath);
//数据服务器
string dataserver = config.GetValue("RemoteSQLServerUri");
//数据服务器用户名称
string user = config.GetValue("RemoteSQLServerUser");
//数据服务器密码
string pwd = config.GetValue("RemoteSQLServerPWD");
//采用的数据库
string database=config.GetValue("RemoteSQLDataBase");

return "data source="+dataserver+"; user id="+user+"; initial catalog="+database+";password="+pwd;

}

}
}
...全文
190 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ivony 2006-08-22
  • 打赏
  • 举报
回复
1、楼主的问题原因是,Page的Request属性其实是HttpAplication实例在调用Page.ProcessRequest的时候把HttpContext传进去之后才有的,所以Page在没有调用ProcessRequest的时候是不能使用Request属性的。

2、HttpApplication.Start事件发生在所有请求发生前,所以这个时候没有HttpContext,只有在客户端发出请求并由HttpApplication处理的时候才会有HttpContext。
www_123du_com 2006-08-22
  • 打赏
  • 举报
回复
我在我项目中有以下代码,你参考一下:
protected void Application_Start(Object sender, EventArgs e)
{
Server.MapPath(this.Context.Request.ApplicationPath)+@"abc.txt";
}
www_123du_com 2006-08-21
  • 打赏
  • 举报
回复
嘿,老兄,你看错了,是Ivony()答的,我只是重发一遍。要看清楚呀,我遭遇这种事情就很苦恼!
--------------------
namespace Dottext.Framework
{
public class Idata
{
private void author(object source, ElapsedEventArgs e)
{
string filename = HttpContext.Current.Server.MapPath("" + recommendjs);//出现下述错误
}
}
}
错误:An exception of type 'System.NullReferenceException' occurred in Dottext.Web.DLL but was not handled in user code
Additional information: 未将对象引用设置到对象的实例。
我在Global.asax中的Application_Start方法中调用它,出现上述错误,用Request.ApplicationPath 取路径依然是这样的错误,请问您知道其中的原因吗,如果知道一定帮我这个小菜鸟一把,万分感谢
----------------------
对这个问题我也不清楚,我以前遇到过这问题,但好像记得用HttpContext.Current访问就没问题了的,不清楚你的具体原因。
CNBLOCK 2006-08-21
  • 打赏
  • 举报
回复
谢谢
www_123du_com 2006-08-21
  • 打赏
  • 举报
回复
System.Web.UI.Page page=new System.Web.UI.Page();
_________________________________________________________

呵,这样肯定不行!像Ivony()这样做:HttpContext.Current.Request
Ivony 2006-08-21
  • 打赏
  • 举报
回复
HttpContext.Current.Request

在Page的构造含树里,Request和Response等属性是无效的……

62,046

社区成员

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

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

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

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