Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 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 May 12th, 2005, 10:54 AM
Authorized User
 
Join Date: May 2005
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default Trying to map a path in a class

Hi

I've written a class (which is imported into a control sitting on my default.aspx) as follows:

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.Control
Imports System.Web.HTTPContext
Imports System.Web.HttpRequest

Namespace TourCode

    Public Class TourCodes
        Function getTourCode() As String
            Dim strPath As String = Page.Request.Path
            Dim intStart As Integer = strPath.LastIndexOf("/") -2
            Dim intLength As Integer = 2

            Dim strTourCode As String = strPath.Substring(intStart,intLength)

            If Current.Session("TourCode") <> strTourCode Then
               Current.Session("TourCode") = strTourCode
            End If

            Return strTourCode
        End Function

    End Class

End Namespace


The aim of the class is to simply map the path, and store the folder name as a session variable. The problem I have is that I'm not sure of HOW I can actually map the path. Using Page.Request.Path only works if I "Inherit Control" in the Class. However, by doing this I get an error in the control (which I am importing this into). Can someone tell me how I would simply map the path? I've tried Page.Request.Path, I have tried to Import the namespace HTTPRequest etc... Is there an easy way to do this?

Thanks

 
Old May 12th, 2005, 11:59 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

You can use: HttpContext.Current.Server.MapPath()

Alternatively, you can use AppDomain.CurrentDomain.BaseDirectory which returns the base directory for the current application.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old May 13th, 2005, 04:59 AM
Authorized User
 
Join Date: May 2005
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Imar

Both work perfectly as you've described! There is just one thing though (my wording wasn't 100% precise in my last post).

If I use HttpContext.Current.Server.MapPath(), then I have to put in a file name in order for it to map to that destination. The real problem is I want this to work on every page in different directories, so I'd much prefer to use some kind of "Page.Request.Path" equivalent. I'd like something that returns the complete path directory, including the file name, ie, so that I don't actually need to map it, it just gives me the complete directory path.

Frustratingly, your other suggestion AppDomain.CurrentDomain.BaseDirectory, stops just short of the information I am hoping to receive. I need the next level up from the BaseDirectory!

Do you have any other suggestions?

Dank je wel!







 
Old May 13th, 2005, 10:18 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

From the other thread of yours, my understanding is that you want to look at the URL path. You can access this with one of several server variables:

SCRIPT_NAME will return something like /myapplication/mypage.ext
PATH_INFO is usually the same as SCRIPT_NAME
PATH_TRANSLATED returns the complete local path of the requested file (but from what I understand you need just the URL parts, not the local file)

Take a look at the trace.axd virtual page in your web application:

http://localhost/<your application name/trace.axd

As long as you have tracing enabled in your application you can see the last several requests and look at the trace details for them. Towards the bottom are all the server variables.

-Peter
 
Old May 17th, 2005, 06:36 AM
Authorized User
 
Join Date: May 2005
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Peter

Thanks once again for your help. Funnily enough, I was trying to use Server Variables and "PATH_INFO" but was trying the following:

Dim strPath As String = Request.ServerVariables("PATH_INFO")

And was consequently getting the error "Referene to a non-shared member requires an object reference".

However, after reading your posting and realising that it COULD be done, I tried the following:

Dim strPath As String = Current.Request.ServerVariables("PATH_INFO")

and it works perfectly. At the moment, I don't really understand the System.Web.HTTPContext and HTTPContext.Current, so this will be something I think I need to look into more. It seems to be the solutions to a lot of my problems lately...

Thanks once again for the help, and apologies for my slow reply - I had a long weekend!



 
Old May 17th, 2005, 11:24 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I can explain:

When you are working in a webform (either the webform codebehind class or inline script directly in the ASPX file) you are within the context of a class file that derives from System.Web.UI.Page. That class has a visible property named "Server" as well as "Response" and "Request". These properties give you access to live instances of the class behind each property because the expose member instances living in the System.Web.UI.Page class instance created for the page request.

When you create a helper class with methods that you wish to access Server, Response and/or Request you need a valid context of each. The helper class itself is apart from the Page class you are consuming it in so it has no idea of how to get a valid context (unlike any page instance where it's just a property name away). Therefore, you are left with 1 of two options:

1. Your helper method must accept an instance of the class(es) you need to use:

   MyHelperClass.HelperSub(oHttpContext As HttpContext)

The helper method can now access the current HttpContext because you require one to be passed into the method.

2. Your helper method uses the .Current property of the HttpContext class which returns a valid HttpContext instance for the executing page request:

   System.Web.HTTPContext.Current.Request...

.Current is nothing more than a static/shared member property of type HttpContext. This can be a little confusing because the HttpContext class has a property that returns an instance of itself. This is perfectly legal as long as you don't get yourself into a situation where you get into a infinite loop such as something like this:

Class MyClass
  Private _objMyClass As MyClass 'This is OK

  Public Sub New()
    'This is where we get into trouble
    _objMyClass = New MyClass
  End Sub
End Class

This would result in a stack overflow because you'd end up with a infinitely recursing constructor.

-Peter
 
Old May 19th, 2005, 04:43 AM
Authorized User
 
Join Date: May 2005
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Peter,

Thank you so much for that! It's starting to all come together now which is a great feeling. I really appreciate all the help that you've given me with this project.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Map Path??? thetwai C# 3 April 5th, 2007 07:26 AM
Error: File path and URL http map differently cJeffreywang ASP.NET 1.0 and 1.1 Basics 4 March 27th, 2007 08:03 PM
FSO Map.Path points to development site nancy Classic ASP Basics 0 April 27th, 2005 01:23 PM
Class path setting pedr0 BOOK: Beginning Java 2 5 July 26th, 2004 03:36 PM





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