How to change Global.asax to code-behind

Discussion in 'ASP .Net' started by ad, Sep 10, 2005.

  1. ad

    ad Guest

    The Global.asax is code-inside with default.
    How to change Global.asax to code-behind?
    ad, Sep 10, 2005
    #1
    1. Advertising

  2. ad

    Scott Allen Guest

    In global.asax you need to modify the @ Application directive.

    <%@ Application
    Codebehind="Global.asax.cs" Inherits="YourNamespace.Global" %>

    Then create your .asax.cs file, and add a class named Global derived
    from System.Web.HttpApplication.

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/

    On Sat, 10 Sep 2005 21:17:22 +0800, "ad" <>
    wrote:

    >The Global.asax is code-inside with default.
    >How to change Global.asax to code-behind?
    >
    Scott Allen, Sep 10, 2005
    #2
    1. Advertising

  3. AFAIK, that's gone from ASP.NET/VS.NET 2.0, though.

    The new Web project model affects the Global.asax file.

    In 2.0, when you convert a previous project :

    1. the global.asax code-behind file's contents are moved to the App_Code directory.

    2. The CodeBehind and Inherits attributes are removed from the @ Page directive.

    3. A Language attribute is added to the @ Page directive, if one is not already specified.

    4. For Visual Basic, a Namespace statement is added to the class file.
    The namespace is defined by the root namespace in the Web project.

    VS.NET 2005 won't even let you create a global.asax
    with a code-behind file in any new website you create.

    See : http://msdn2.microsoft.com/en-us/library/421whcfb(en-us,vs.80).aspx

    The section titled "Global.asax File"




    Juan T. Llibre
    ASP.NET MVP
    ASP.NET FAQ : http://asp.net.do/faq/
    ==========================

    "Scott Allen" <> wrote in message
    news:...
    > In global.asax you need to modify the @ Application directive.
    >
    > <%@ Application
    > Codebehind="Global.asax.cs" Inherits="YourNamespace.Global" %>
    >
    > Then create your .asax.cs file, and add a class named Global derived
    > from System.Web.HttpApplication.
    >
    > --
    > Scott
    > http://www.OdeToCode.com/blogs/scott/
    >
    > On Sat, 10 Sep 2005 21:17:22 +0800, "ad" <>
    > wrote:
    >
    >>The Global.asax is code-inside with default.
    >>How to change Global.asax to code-behind?
    >>

    >
    Juan T. Llibre, Sep 10, 2005
    #3
  4. ad

    Scott Allen Guest

    That's an odd piece of documentation you found, Juan. I'm not sure why
    global.asax would ever have an @ Page directive.

    I know VS2005 doesn't let you create a global.asax with a code-behind
    file, but adding the CodeBehind and Inherits attributes and putting
    the .cs file in App_Code does work - I've tried it. There is not much
    of a benefit in doing so, though.

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/

    On Sat, 10 Sep 2005 11:06:33 -0400, "Juan T. Llibre"
    <> wrote:

    >AFAIK, that's gone from ASP.NET/VS.NET 2.0, though.
    >
    >The new Web project model affects the Global.asax file.
    >
    >In 2.0, when you convert a previous project :
    >
    >1. the global.asax code-behind file's contents are moved to the App_Code directory.
    >
    >2. The CodeBehind and Inherits attributes are removed from the @ Page directive.
    >
    >3. A Language attribute is added to the @ Page directive, if one is not already specified.
    >
    >4. For Visual Basic, a Namespace statement is added to the class file.
    >The namespace is defined by the root namespace in the Web project.
    >
    >VS.NET 2005 won't even let you create a global.asax
    >with a code-behind file in any new website you create.
    >
    >See : http://msdn2.microsoft.com/en-us/library/421whcfb(en-us,vs.80).aspx
    >
    >The section titled "Global.asax File"
    >
    >
    >
    >
    >Juan T. Llibre
    >ASP.NET MVP
    >ASP.NET FAQ : http://asp.net.do/faq/
    >==========================
    >
    >"Scott Allen" <> wrote in message
    >news:...
    >> In global.asax you need to modify the @ Application directive.
    >>
    >> <%@ Application
    >> Codebehind="Global.asax.cs" Inherits="YourNamespace.Global" %>
    >>
    >> Then create your .asax.cs file, and add a class named Global derived
    >> from System.Web.HttpApplication.
    >>
    >> --
    >> Scott
    >> http://www.OdeToCode.com/blogs/scott/
    >>
    >> On Sat, 10 Sep 2005 21:17:22 +0800, "ad" <>
    >> wrote:
    >>
    >>>The Global.asax is code-inside with default.
    >>>How to change Global.asax to code-behind?
    >>>

    >>

    >
    >
    Scott Allen, Sep 11, 2005
    #4
  5. re:
    > I'm not sure why global.asax would ever have an @ Page directive.


    The doc refers to the changes made by project *conversions*.
    As usual with MS documentation, you need to interpret it. ;-)

    re:
    > adding the CodeBehind and Inherits attributes
    > and putting the .cs file in App_Code does work


    Yup, but the explanation is nowhere to be found,
    *except in that document*.

    I, also, hate the idea of putting source code on a server.

    I don't know who came up with that doozy, but they should
    be hung by their you-know-whats for inflicting that on us.

    I'm skipping that part entirely by manually compiling to assemblies
    anything which would go into App_Code as raw code.

    I see the App_Code directory as a security failpoint.

    I'm not sure if there's any advantage any more to using
    codebehind, anyway, particularly with global.asax.

    If you can do anything with inline coding, or manually-compiled assemblies,
    that you can do with codebehind, why should we have to go through the
    additional contortions which codebehind requires ?

    Do you know of anything which can be done in codebehind
    which can't be done inline or with command-line compiled assemblies ?

    I'm finding the codebehind model a bit hard to swallow these days
    primarily because of the additional complexity which it requires,
    and the usual explanation that it "separates UI from code" sounds,
    increasingly, hollow.

    I can separate UI from code with manually-compiled assemblies.



    Juan T. Llibre
    ASP.NET MVP
    ASP.NET FAQ : http://asp.net.do/faq/
    ==========================

    "Scott Allen" <> wrote in message
    news:...
    > That's an odd piece of documentation you found, Juan. I'm not sure why
    > global.asax would ever have an @ Page directive.
    >
    > I know VS2005 doesn't let you create a global.asax with a code-behind
    > file, but adding the CodeBehind and Inherits attributes and putting
    > the .cs file in App_Code does work - I've tried it. There is not much
    > of a benefit in doing so, though.
    >
    > --
    > Scott
    > http://www.OdeToCode.com/blogs/scott/
    >
    > On Sat, 10 Sep 2005 11:06:33 -0400, "Juan T. Llibre"
    > <> wrote:
    >
    >>AFAIK, that's gone from ASP.NET/VS.NET 2.0, though.
    >>
    >>The new Web project model affects the Global.asax file.
    >>
    >>In 2.0, when you convert a previous project :
    >>
    >>1. the global.asax code-behind file's contents are moved to the App_Code directory.
    >>
    >>2. The CodeBehind and Inherits attributes are removed from the @ Page directive.
    >>
    >>3. A Language attribute is added to the @ Page directive, if one is not already
    >>specified.
    >>
    >>4. For Visual Basic, a Namespace statement is added to the class file.
    >>The namespace is defined by the root namespace in the Web project.
    >>
    >>VS.NET 2005 won't even let you create a global.asax
    >>with a code-behind file in any new website you create.
    >>
    >>See : http://msdn2.microsoft.com/en-us/library/421whcfb(en-us,vs.80).aspx
    >>
    >>The section titled "Global.asax File"
    >>
    >>
    >>
    >>
    >>Juan T. Llibre
    >>ASP.NET MVP
    >>ASP.NET FAQ : http://asp.net.do/faq/
    >>==========================
    >>
    >>"Scott Allen" <> wrote in message
    >>news:...
    >>> In global.asax you need to modify the @ Application directive.
    >>>
    >>> <%@ Application
    >>> Codebehind="Global.asax.cs" Inherits="YourNamespace.Global" %>
    >>>
    >>> Then create your .asax.cs file, and add a class named Global derived
    >>> from System.Web.HttpApplication.
    >>>
    >>> --
    >>> Scott
    >>> http://www.OdeToCode.com/blogs/scott/
    >>>
    >>> On Sat, 10 Sep 2005 21:17:22 +0800, "ad" <>
    >>> wrote:
    >>>
    >>>>The Global.asax is code-inside with default.
    >>>>How to change Global.asax to code-behind?
    >>>>
    >>>

    >>
    >>

    >
    Juan T. Llibre, Sep 11, 2005
    #5
  6. ad

    Scott Allen Guest

    On Sat, 10 Sep 2005 21:39:56 -0400, "Juan T. Llibre"
    <> wrote:

    >
    >If you can do anything with inline coding, or manually-compiled assemblies,
    >that you can do with codebehind, why should we have to go through the
    >additional contortions which codebehind requires ?
    >
    >Do you know of anything which can be done in codebehind
    >which can't be done inline or with command-line compiled assemblies ?
    >


    I can't think of anything you can or can't do in one model that you
    can do in the other.

    I was sticking with code-behind based on the close-minded principal
    that it was "the right way to do it".

    The more I learned abouyt 2.0 the more I started liking inline code -
    it's one less file to manage, one less file to "diff" ... etc. There
    is no difference in productivity or tool support in 2.0!

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/
    Scott Allen, Sep 11, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?YmNoYXJsZXM=?=

    global.asax and global.asax.cs

    =?Utf-8?B?YmNoYXJsZXM=?=, Oct 4, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    820
    =?Utf-8?B?YmNoYXJsZXM=?=
    Oct 5, 2004
  2. Jon Maz

    Global.asax - Code Behind or Inline?

    Jon Maz, Oct 13, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    5,036
    Scott Allen
    Oct 13, 2004
  3. =?Utf-8?B?Tm9OaWNrbmFtZQ==?=

    ASP.NET 2/Global.asax/code-behind/Application_Start

    =?Utf-8?B?Tm9OaWNrbmFtZQ==?=, Nov 9, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    6,331
    Juan T. Llibre
    Nov 9, 2005
  4. Mark Rae

    Global.asax / Global.asax.cs in v2

    Mark Rae, May 23, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    3,151
    Mark Rae
    May 23, 2006
  5. Hardy Wang
    Replies:
    4
    Views:
    6,872
    Steven Cheng[MSFT]
    Sep 8, 2006
Loading...

Share This Page