Click to See Complete Forum and Search --> : ASP.NET Strings
kwilliams
01-31-2007, 05:10 PM
I have what's hopefully a pretty simple question about how to pull parts of a string into variables from within an ASP.NET code-behind doc:. I'm currently researching a site at http://www.aspmatrix.com/asp-net/string_class_examples/index.aspx to see if I can figure this out on my own, but I thought I'd also check here for some direction. So here is what I have/need:
1) Pull the page path:
Dim strPagePath As String = Request.ServerVariables("PATH_INFO")
2) There are three path options for an ASP.NET file on my site:
/maindir/page.aspx
/maindir/subdir1/page.aspx
/maindir/subdir1/subdir2/page.aspx
3) I need to pull up to 4 possible variables (see below). This is where I'm running into a question: How can I pull the following variables from the above path when I don't know whether or not the file will be in the main directory (maindir), sub directory 1 (subdir1), and/or sub directory 2 (subdir2) until after the page is requested by the user?
Dim strMainDir As String = "maindir"
Dim strSubDir1 As String = "subdir1"
Dim strSubDir2 As String = "subdir2"
Dim strPageId As String = "page"
Thanks for any and all help.
A1ien51
01-31-2007, 05:35 PM
Split will give you an array.
Eric
kwilliams
02-07-2007, 03:49 PM
Hi A1ien51,
I wanted to let you know that I used your suggestion with some other code to come up with a working solution, and I'd like to post it for any others that may want to use it. So here it is:
Dim strPagePath As String = Request.ServerVariables("PATH_INFO")
Dim fi As FileInfo = New FileInfo(strPagePath)
'Split page path
Dim myArray = Split(strPagePath, "/") 'the delimiter is the slash
'Declare array values
Dim i As Integer
For i = 0 To UBound(myArray) 'the UBound function returns 3
Response.Write(myArray(i) & "<br />") 'test
Next 'move on to the next value of i
'Assign page path values based on number of directories pulled
Select Case UBound(myArray)
Case 0 'default
Response.Write("There are no directories and the user will be redirected to the home page.") 'test
Case 1
Response.Write("There is one directory.") 'test
Case 2
Response.Write("There are two directories.") 'test
Case 3
Response.Write("There are three directories.") 'test
End Select
'Pull and assign strPageId value from filepath
'Dim strPageId = fi.Name 'with extension
Dim strPageId = fi.Name.Replace(fi.Extension, "") 'without extension
With this code I'm able to pull the page's full path, split the directories up, perform tasks based on the number of directories in the array, and pull the page name with or without the extension.
I don't know if it will help anyone, but it never hurts. Thanks again for your help.
Cstick
02-07-2007, 09:56 PM
You could also use System.Web.HttpRequest.Url to get the requested Uri.
Then use the segments property of the Uri class (http://msdn2.microsoft.com/en-us/library/system.uri.segments.aspx).
For a path like "http://www.webdeveloper.com/forum/newreply.php?do=newreply"
The segments property will return an array of strings representing the portion of the path, "/forum/newreply.php".
Try this: (untested)
For Each part As String In Request.Url.Segments
Response.Write(String.Format("Part: {0}<br />",part))
Next
kwilliams
02-08-2007, 09:30 AM
I'll check that option out Cstick. I'm trying to work with the most efficient ways of coding, so that may be a better solution for me. I'll let you know how it works out for me. Thanks.
kwilliams
02-08-2007, 10:44 AM
Hello again Cstick,
I've been researching the segments property of the Uri class, and I've inputed your suggested code into my code with success. Now I'm trying to figure out how to create a count of the number of segments that were pulled in. I tried to use Ubound in a Select Case method, like this:
Select Case UBound(part)
Case 0
Response.Write("There are no directories and the user will be redirected to the home page.") 'test
Case 1
Response.Write("There is one directory.") 'test
Case 2
Response.Write("There are two directories.") 'test
Case 3
Response.Write("There are three directories.") 'test
End Select
...but I received the following error:
'part' is a type and cannot be used as an expression.
Is there a way to count the number of segments returned using your suggested method? If so, could you please point me in the right direction? Thanks.
kwilliams
02-08-2007, 10:53 AM
I just answered my own question. Here is the code that pulls the segment count:
Dim segCount As Integer = MyUrl.Segments.Length
'Assign page path values based on number of directories pulled
Select Case segCount
Case 0
Response.Write("There are no directories and the user will be redirected to the home page.") 'test
Case 1
Response.Write("There is one directory.") 'test
Case 2
Response.Write("There are two directories.") 'test
Case 3
Response.Write("There are three directories.") 'test
End Select
Thanks again for your help.
Cstick
02-08-2007, 12:44 PM
I'm glad that worked out. The .Net framework has a ton of prepared classes for handling routine functions. It just takes time to find them all. Another helpful path helper that isn't well known is System.IO.Path for interrogating physical paths. And System.Web.VirtualPathUtility is another helpful one for dealing with absolute and relative URLs.
Edit: Spell Check - Did you know that Firefox 2.0 has a built in spell checker?
kwilliams
02-08-2007, 12:47 PM
That's good to know. I greatly appreciate the information. It's been very helpful.