.NET Forum / ASP.NET / General / July 2007
What happened to the Global class code behing in 2.0?
|
|
Thread rating:  |
PatB - 16 Jul 2007 14:46 GMT Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax code file.
In 1.1 I could have a code behind file for the global.asax file. This allow for shared variables of the Global class. Note: I use these shared variables for read only values that are set at application start.
It would appear the 2.0 doesn't like you to use shared variables in the global class. How do I convert my 1.1 applications to 2.0 without the ability to have these shared variables?
Why would Microsoft get rid of the global.asax code behind? There must a reason and a good conversion method (I hope).
 Signature Pat B BCC Software, Inc A BÖWE BELL + HOWELL COMPANY
Juan T. Llibre - 16 Jul 2007 15:11 GMT It's not the default any more, but you can still use it.
Read this article : http://www.xerratus.com/2006/10/20/NET20GlobalasaxCodeInSeparateFile.aspx
You need to place all your global.asax code in global.asax.cs, in the App_Code directory
You must include this at the top of global.asax.cs :
public class Global : System.Web.HttpApplication
...and include this directive in global.asax : <%@ Application Language="C#" CodeBehind="Global.asax.cs" Inherits="Global" %>
the same for vb : <%@ Application Language="VB" CodeBehind="Global.asax.vb" Inherits="Global" %>
Download a complete example from :
http://www.xerratus.com/ct.ashx?id=c8a032e5-632c-4027-87e6-06224daa33ab&url=http %3a%2f%2fwww.xerratus.com%2fcontent%2fbinary%2fGlobalCodeBehindWebSiteExample.zi p
Juan T. Llibre, asp.net MVP asp.net faq : http://asp.net.do/faq/ foros de asp.net, en español : http://asp.net.do/foros/ ======================================
> Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax > code file. [quoted text clipped - 9 lines] > Why would Microsoft get rid of the global.asax code behind? There must a > reason and a good conversion method (I hope). PatB - 16 Jul 2007 15:34 GMT Juan,
I implemented the work-around and it appears to be working.
In my 1.1 code I can reference these shared variables by Global.VariableName. In the 2.0 code it appears that I need to reference then with two global references like this: Global.Global.VariableName.
Should it be like this? Is something wrong? This will mean when converting my 1.1. apps I'll have to make a lot of changes to change the Global. reference to Global.Global.
Does anyone know the reasoning why Microsoft removed the code behind model for the global.asax?
 Signature Pat B BCC Software, Inc A BÖWE BELL + HOWELL COMPANY
> It's not the default any more, but you can still use it. > [quoted text clipped - 35 lines] > > Why would Microsoft get rid of the global.asax code behind? There must a > > reason and a good conversion method (I hope). Juan T. Llibre - 16 Jul 2007 15:50 GMT re: !> I implemented the work-around and it appears to be working.
Yes, it does work.
re: !> In the 2.0 code it appears that I need to reference !> then with two global references like this: Global.Global.VariableName.
!> Should it be like this?
I can only suggest for you to look for a Global subclassing of Global in your code. Experiment...and remove one of the Global subclassings.
re: !> Does anyone know the reasoning why Microsoft removed the code behind model for the global.asax?
Because it's wasted effort. You don't need to compile the codebehind first, and then compile the global.asax class again at runtime.
Anything you can do in codebehind you can also do inline, so the codebehind isn't needed. You can still implement it, like I pointed out, but, really, you're only duplicating compilation efforts.
Juan T. Llibre, asp.net MVP asp.net faq : http://asp.net.do/faq/ foros de asp.net, en español : http://asp.net.do/foros/ ======================================
> Juan, > [quoted text clipped - 50 lines] >> > Why would Microsoft get rid of the global.asax code behind? There must a >> > reason and a good conversion method (I hope). Patrice - 16 Jul 2007 15:29 GMT Please never assume anything and ALWAYS post the exact behavior you see (runtime error, compile time error, exact error message). Of course if we try, it will work as we are not in the same condition (my guess would be a namespace change ?)
My personal preference would be to use a specific class (also the configuration class provides an API that will expose values stored in your web application config class, see WebConfigurationManager).
-- Patrice
> Just starting to move to ASP.NET 2.0 and having trouble with the > Global.asax [quoted text clipped - 12 lines] > Why would Microsoft get rid of the global.asax code behind? There must a > reason and a good conversion method (I hope). bruce barker - 16 Jul 2007 15:30 GMT there is no need for a codebehind file. you just put the code in global.asax file. as there is nothing but the code in the global.asax file, 2 files don't make much sense.
-- bruce (sqlwork.com)
> Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax > code file. [quoted text clipped - 9 lines] > Why would Microsoft get rid of the global.asax code behind? There must a > reason and a good conversion method (I hope). PatB - 16 Jul 2007 15:50 GMT Bruce,
Try to add a shared variable? You can't do it without a code behind file with a class definition. At least I couldn't get it to do it.
 Signature Pat B BCC Software, Inc A BÖWE BELL + HOWELL COMPANY
> there is no need for a codebehind file. you just put the code in > global.asax file. as there is nothing but the code in the global.asax [quoted text clipped - 15 lines] > > Why would Microsoft get rid of the global.asax code behind? There must a > > reason and a good conversion method (I hope). Juan T. Llibre - 16 Jul 2007 16:47 GMT re: !> Try to add a shared variable? You can't do it without a code behind file !> with a class definition. At least I couldn't get it to do it.
Why can't you use an Application Variable to do that ?
Juan T. Llibre, asp.net MVP asp.net faq : http://asp.net.do/faq/ foros de asp.net, en español : http://asp.net.do/foros/ ======================================
> Bruce, > [quoted text clipped - 20 lines] >> > Why would Microsoft get rid of the global.asax code behind? There must a >> > reason and a good conversion method (I hope). PatB - 16 Jul 2007 17:16 GMT I could use an application variable, but they are not strongly typed which requires a conversion every time you use it.
Shared variables of the global class are typed and do not require a conversion.
 Signature Pat B BCC Software, Inc A BÖWE BELL + HOWELL COMPANY
> re: > !> Try to add a shared variable? You can't do it without a code behind file [quoted text clipped - 30 lines] > >> > Why would Microsoft get rid of the global.asax code behind? There must a > >> > reason and a good conversion method (I hope). Juan T. Llibre - 16 Jul 2007 17:51 GMT re: !> Shared variables of the global class are typed and do not require a conversion.
To each his own, depending on his coding preferences, I guess.
I wouldn't sacrifice performance for a coding preference, though.
Even though the difference is not really critical, compiling somthing twice doesn't appeal much to me.
;-)
Juan T. Llibre, asp.net MVP asp.net faq : http://asp.net.do/faq/ foros de asp.net, en español : http://asp.net.do/foros/ ======================================
>I could use an application variable, but they are not strongly typed which > requires a conversion every time you use it.
> Shared variables of the global class are typed and do not require a conversion. > [quoted text clipped - 32 lines] >> >> > Why would Microsoft get rid of the global.asax code behind? There must a >> >> > reason and a good conversion method (I hope). Steven Cheng[MSFT] - 17 Jul 2007 04:48 GMT Thanks for Juan's input.
Hi Pat,
As Juan mentioned, due to the new dynamic compilation model, in ASP.NET 2.0, it prefer to use the inline code model for global.asax. However, you can still use a codebehind class(define in App_Code dir) and inherit it in @Application directive.
Or you can also direclty put a separate partial class file(ASP.NET 2.0 specific model) with the global.asax(named global.asax.cs). e.g.
====in global.asax=========== <%@ Application Language="C#" CodeFile="Global.asax.cs" Inherits="Global" %>
====global.asax.cs========= public partial class Global : HttpApplication { ....... ============================
In addition, for sharing some global variables, in ASP.NET 2.0 application, I think you can consider using the following means instead of define them in global class:
You can create a dedicated utility class(mark as public) and define some static(shared) member variables in it so that other pages can access them. How do you think?
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
Steven Cheng[MSFT] - 19 Jul 2007 15:40 GMT Hi Pat,
Have you got any further idea on this? If you have any other questions, please feel free to post here.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
Peter Bromberg [C# MVP] - 16 Jul 2007 15:42 GMT If you're "just starting" to move into ASP.NET 2.0, then it's not too late to start developing good habits. One that I've gotten is NOT to use the Web Site Project model, but the Web Application Project model for building my "stuff". The original global.asax codebehind arrangement is preserved. My humble opinion, of course. -- Peter Site: http://www.eggheadcafe.com UnBlog: http://petesbloggerama.blogspot.com BlogMetaFinder(BETA): http://www.blogmetafinder.com
> Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax > code file. [quoted text clipped - 9 lines] > Why would Microsoft get rid of the global.asax code behind? There must a > reason and a good conversion method (I hope). Juan T. Llibre - 16 Jul 2007 15:52 GMT Ah, my turn to say to you : good point, Peter. WAP can work that way.
I still prefer inline code, though. I use it when building WAP apps.
;-)
Juan T. Llibre, asp.net MVP asp.net faq : http://asp.net.do/faq/ foros de asp.net, en español : http://asp.net.do/foros/ ======================================
> If you're "just starting" to move into ASP.NET 2.0, then it's not too late to > start developing good habits. One that I've gotten is NOT to use the Web Site [quoted text clipped - 19 lines] >> Why would Microsoft get rid of the global.asax code behind? There must a >> reason and a good conversion method (I hope). PatB - 16 Jul 2007 15:56 GMT Peter,
I created a ASP.NET AJAX-enabled Web application.
Your comments confuse me. What's the difference between Web Site Project model amd the Web Application Project model? How do I switch or create a Web Application Project Model? Am I missing something during the project creation?
Thank you,
 Signature Pat B BCC Software, Inc A BÖWE BELL + HOWELL COMPANY
> If you're "just starting" to move into ASP.NET 2.0, then it's not too late to > start developing good habits. One that I've gotten is NOT to use the Web Site [quoted text clipped - 19 lines] > > Why would Microsoft get rid of the global.asax code behind? There must a > > reason and a good conversion method (I hope). Peter Bromberg [C# MVP] - 16 Jul 2007 16:58 GMT let's deal with one issue at a time or we will all get confused! Web Site Project - the original ASP.NET model that shipped with Visual Studio 2005. Web Application Project - the new model that is similar to Visual Studio 2003 (has a project file, builds a single dll assembly). This was an add-on delivered in 2006 and is now standard as of Service Pack 1.
AJAX projects are a separate issue, but you should still be able to do the codebehind as explained by others in this thread. -- Peter Site: http://www.eggheadcafe.com UnBlog: http://petesbloggerama.blogspot.com BlogMetaFinder(BETA): http://www.blogmetafinder.com
> Peter, > [quoted text clipped - 29 lines] > > > Why would Microsoft get rid of the global.asax code behind? There must a > > > reason and a good conversion method (I hope).
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|