global.asax

  • Thread starter Justin Morris via DotNetMonster.com
  • Start date
J

Justin Morris via DotNetMonster.com

i am starting to develop some websites using ASP.NET, and would like
clearification on global.asax and web.config.

I have several variables that are specific to certain websites, Admin
Email, Company Name, Company Phone, Address...so on. I would like to store
these some place and call them throughout the web pages or applications.
These pages and apps are being used by several sites but the actions don't
change.

Example = Newsletter sign up -> the same application just changing the from
email address and website name on the auto email sent to user.

Example = Footer. The footer page is the same just change the company name
and address.

Instead of changing each instance of the company name or email in the
webpages I would like store a list of variables in one place. Should this
be in the global.asax or web.config. I don't think a database is necessary
because these variables shouldn't change (often) once the site is setup
 
B

Brock Allen

I have several variables that are specific to certain websites, Admin
Email, Company Name, Company Phone, Address...so on. I would like to
store these some place and call them throughout the web pages or
applications. These pages and apps are being used by several sites but
the actions don't change.

These sorts of things tend to go in web.config. You can use the <appSettings>
section to store those simple key/value pairs, or you can create a more ellaborate
XML block in the web.config. Here are the docs for <appSettings> and the
API to read it:

http://msdn.microsoft.com/library/d...onfigurationSettingsClassAppSettingsTopic.asp
Example = Footer. The footer page is the same just change the company
name and address.

Now this might be slightly different. If you want common HTML then a user
control (ASCX file) is the way to achieve that. This is the new way server-side
includes are done in ASP.NET (if you have an ASP background, then that will
make sense). From the user control you can access the data you've stored
in web.config from above. Docs on user controls:

http://msdn.microsoft.com/library/d...n/html/vbconintroductiontowebusercontrols.asp
 
J

Justin Morris via DotNetMonster.com

i understand the includes/ascx files which i use to include the header and
footer for each aspx page. I meant in the footer, almost always you see:

Copyright ?2005 <somecompany> All rights reserved. Use of this site is
subject to certain terms and conditions.

If i copy the core pages of one site to another site, I would like to
change the CompanyName in one spot and for each and every useage of the
CompanyName would updated regaurdless of being in the footer/header/auto
emails/page text. It seems like I am developing simple sites for people
that are the same basic functions or skeleton. It gets repetitive changing
email address, phone, names while the majority of the code stays the same.

Could I not use the global.asax file like:

Sub Application_Start(Sender As Object, E As EventArgs)
Application("CompanyName") = "Company1"
End Sub

And then call <%=Application("CompanyName")%> in the pages.

Copyright ?2005 <%=Application("CompanyName")%> All rights reserved. Use of
this site is subject to certain terms and conditions.

Why would web.config be better?

Thank you-
 
B

Brock Allen

You could use the Application object, just like it was done in classic ASP.
web.config is more "standard", I suppose, with ASP.NET though. Both can accomplish
the same goal.
 
J

Justin Morris via DotNetMonster.com

could you explain the other way, i didn't quite understand the msdn article.

Why is web.config is more "standard"?

how do i set the variable in web.config and then call it on the page?

Thank you
 
B

Brock Allen

Why is web.config is more "standard"?

web.config is a dedicated confguration file. it's text (xml) and it's meant
to be editable outside of the application by an admin type. global.asax is
code. only devs should be working with that. If you're using codebehind it's
possible you don't really have much of anything in global.asax since it's
compiled into a DLL. the idea is that the external configuration file doesn't
require a recompile to change the settings since it can be edited with notepad.
how do i set the variable in web.config and then call it on the page?

In web.config:
<configuration>
<appSettings>
<add key="CompanyName" value="Acme Corp., Inc" />
</appSettings>
</configuration>

In your code:
// assumes a using System.Configuration; at the top of the file
myLabel.Text = ConfigurationSettings.AppSettings["CompanyName"];

There ya go :)
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top