Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 31st, 2007, 06:09 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default ASP.NET Strings

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/str...les/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.

KWilliams
 
Old January 31st, 2007, 07:58 PM
Authorized User
 
Join Date: Apr 2005
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Interesting...

You want to be able to seperate out the path into seperate variable depending on 1 out of 4 situations.

I am going to type out the answer in C# but should be easy for you to convert to VB. I will also try to adhoc some VB but keep in mind I can ready it but never really tried to write it hehe.

First thing you want to do is determine what scenario you are dealing with. Feel free to shoot me down if I am stray from what you are trying to do.

I see you have "()" around the defining property index for ServerVariables. It is a property and should use "[]" instead, but if that part is working then above must just be a type hehe.

Start but seperating out all the strings.

string[] arrMyString = strPagePath.Split('//'); //This will split path up into an array split by the backslashes.

Next, you will want to get the string count. Reason being for 2 reasons. One reason is you kave a set number of instances so this gives you something static to look for. You can only have an array that can have 2, 2, or 4 items inside of it. This will be easier handled by a switch statement.

switch(arrMyString.Count)
{
     case 2:
          strMainDir = arrMyString[0];
          strPageId = arrMyString[1];
          break;
}

Declare the string variables before the switch statement or they will go bye bye after you exit switch block if declared inside.

For the other cases just start from 3 and go up to 4. I personally think it would be cool to leave it in the array and pull what you need from there. I hope that made sense. I tend to shorthand a lot when I post versus speaking it hehe.

 
Old February 1st, 2007, 11:14 AM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Quick209,

Before I received your replies, I received a reply yesterday from this forum: http://www.webdeveloper.com/forum/sh...352#post706352

...and this is what I did with that suggestion:

'Declare page path
Dim strPagePath As String = Request.ServerVariables("PATH_INFO")
Dim myArray = Split(strPagePath, "/") 'the delimiter is the slash
'Display the split string
Dim i
For i = 0 To UBound(myArray) 'the UBound function returns 3
    Response.Write(myArray(i) & "<br>")
Next 'move on to the next value of i

This solution works, but I'd like to hear your opinions on whether or not it's the best and most-efficient solution when it comes to coding properly in ASP.NET/VB.NET. Thanks for your help and input.

KWilliams
 
Old February 7th, 2007, 04:43 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi again Quick209,

I wanted to let you know that I used some of your 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:
Code:
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.

KWilliams





Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET 3.5 answers in Professional ASP.NET 3.5 jminatel ASP.NET 3.5 Professionals 1 August 22nd, 2011 07:34 AM
Turning a regular ASP.NET App into an AJAX ASP.Net donrafeal7 Ajax 2 August 31st, 2007 12:33 AM
Database diagram in .net using asp.net,c#.net scotttiger ADO.NET 2 February 16th, 2005 01:35 AM
Beginning ASP.Net Databases Using VB.Net ISBN 6195 tlamazares SQL Server ASP 1 December 15th, 2003 01:28 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.