adding and maintaining named strings in a seperate class file in asp.net
質問
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.
解決
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 override load_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.