Help referencing a variable in HTML of an aspx page

S

simon

hello,
what i'm looking to do is store the path of the app on a the server
for reuse in the site.
my thoughts so far are...
-make a key in the web.config file
-retrieve the value in globals.asax in application startup
-store it in a variable that can be reference from all pages
-use that variable on many pages
my questions
1) what is the syntax of the lines in global.asax page to put this
value into a variable that is accessible to other site pages
2) what is the syntax of using a variable in an aspx page (that may or
may not be part of a .net object - straight html).
for example to use this variable as the first part of a path..
<% rootDir %> & 'images/logo.gif'

thanks for any tips
 
N

Nathan Sokalski

My recommendation is to add a key in the web.config file. The syntax for
this is:

web.config file:

<configuration>
<appSettings>
<add key="baseURL" value="http://localhost/WebApplication1" />
</appSettings>

VB.NET code:

ConfigurationSettings.AppSettings("baseURL")


If you don't want to use the web.config file, you can place it in the
Global.asax.vb file as part of the Global class. You would declare the
variable the same as any other variable that you want to share with other
classes, so you would need to use an appropriate Access Modifier, such as
Public (there are others, look at them to see which one is most appropriate
for your use and are most comfortable with). Here is an example:

Public Const baseurl As String = "http://localhost/WebApplication1"
Public Shared baseurl2 As String = "http://localhost/WebApplication1"

Whether you want to make it a Const is your choice. When you use a constant,
the compiler replaces the anywhere you use the variable with the actual
value, so if you were to ever change the value of the Const you would need
to recompile any code that uses the variable as well (depending on how you
have your code organized this may or may not affect you, and it depends on
whether you expect to ever need to change the value of the Const). Either
way, to access the variable, use this code:

Global.baseurl
Global.baseurl2

I recommend, as I said earlier, the web.config method, it is used more often
for things of this sort. However, the choice is obviously up to you. Good
Luck!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top