The first response suggested: “Using Page.GetType().Name will return "YourPageName_aspx". Replace the underscore with a dot.” Interesting idea but the filename might contain other underscores, so I'd have to be sure to only replace that last one.
The second response provide a means of iterating through all of the ServerVariables:
I wasn't aware of the NameValueCollection, so I'll have to look into that for future use. But I don't want to rely on accessing a named hash inside the server variables.using System.Collections.Specialized;
int loop1, loop2;
NameValueCollection coll;
// Load ServerVariable collection into NameValueCollection object.
coll = Request.ServerVariables;
// Get names of all keys into a string array.
String[] arr1 = coll.AllKeys;
for (loop1 = 0; loop1 < arr1.Length; loop1++)
{
Response.Write("Key: " + arr1[loop1] + "
");
String[] arr2 = coll.GetValues(arr1[loop1]);
for (loop2 = 0; loop2 < arr2.Length; loop2++)
{
Response.Write("Value " + loop2 + ": " + arr2[loop2] + "
");
}
}
Since the first suggestion is a parse anyway, I decided to work with the seemingly appropriate FilePath property on the Request object instead of the type. The page doesn't use regular expressions for anything else, so I settled using String methods:
private string GetPageName()
{
String localFile = Context.Request.FilePath;
if (localFile.Contains("/"))
{
Int32 position = localFile.LastIndexOf("/");
localFile = localFile.Substring(position + 1);
}
return localFile;
}
No comments:
Post a Comment