在 asp.net 中的单独类文件中添加和维护命名字符串
我开发了一个包含 40-50 个页面的 ASP.NET Web 应用程序。我已经使用 Log4net 实现了记录器,并且我的每个页面在其 page_load 事件中都有此方法。
protected void Page_Load(object sender, EventArgs e)
{
try
{
log.Debug("In Page Load Function");
if (!IsPostBack)
{
if (Session["OrgId"] != null)
{
// my logic
}
else
{
Response.Redirect("Homepage.aspx?Sid=1", false);
}
log.Debug("Page load function successfull");
}
}
catch (Exception ex)
{
log.Error(ex.Message, ex);
}
}
此方法有许多字符串,例如 "In page Load function","OrgId","Homepage.aspx?Sid=1","Page load功能成功”
。现在我想将这些在我的应用程序中常见的字符串保留在一个单独的类文件中,并将其与变量一起使用...
您能建议什么类可以用于此目的或您的处理方式吗?
I have developed an asp.net web application containing 40-50 pages. I have implemented logger using Log4net and each of my pages has this in its page_load event
protected void Page_Load(object sender, EventArgs e)
{
try
{
log.Debug("In Page Load Function");
if (!IsPostBack)
{
if (Session["OrgId"] != null)
{
// my logic
}
else
{
Response.Redirect("Homepage.aspx?Sid=1", false);
}
log.Debug("Page load function successfull");
}
}
catch (Exception ex)
{
log.Error(ex.Message, ex);
}
}
This method has many strings like "In page Load function","OrgId","Homepage.aspx?Sid=1","Page load function successfull"
. Now i would like to maintain these string which are common throughout my application in a seperate class file and use it with a variable...
Can you suggest what class can be used for this or your way of handling this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建一个具有此功能的基页,其中
// mylogic
添加对抽象方法的调用(例如load_page
)。然后从此所有其他页面子类化并覆盖它们的load_page
,使其包含您需要的所有逻辑。这会将所有这些字符串保留在一处,尽管我不明白为什么您需要将它们更改为变量。
You can create a base page that has this function, where you have
// my logic
add a call to an abstract method (load_page
, for instance). Then subclass all of your other pages from this and overrideload_page
from them, have it containing all the logic you need.This will keep all of these strings in one place, though I don't see why you would need to change them to variables.
也许将字符串存储在资源文件中会有所帮助。示例此处。
您还可以考虑从基类继承该行为。
Maybe storing your strings in a resource file would help. Example here.
You could also think of inheriting that behavior from a base class.