web.config in a class

Discussion in 'ASP .Net' started by Jerry C, Oct 6, 2008.

  1. Jerry C

    Jerry C Guest

    I need to access the web.config file in a class. I am now using the line:

    rootWebConfig1 = WebConfigurationManager.OpenWebConfiguration("~")

    to get a reference to the config file. The question is the correct way to
    get the config file in classes. There is only one config file and it is in
    the root of the web site. I do not want to use a path since the location of
    the web site can change. The classes will be in the App_Code dir.



    Thank you,

    --
    Jerry
    Jerry C, Oct 6, 2008
    #1
    1. Advertising

  2. Jerry C

    Steve Guest

    Your cide looks correct to me. You are using the tilde to indicate the root
    directory, which is always relative to the site, so should work wherever you
    put the code. What problem are you having with this?

    Also, are you planning to read or write the web.config? Note that changes in
    the web.config file will cause the Web server to restart, and will also
    refresh the cache entries, which may cause problems in your application

    HTH

    "Jerry C" <> wrote in message
    news:...
    >
    > I need to access the web.config file in a class. I am now using the line:
    >
    > rootWebConfig1 = WebConfigurationManager.OpenWebConfiguration("~")
    >
    > to get a reference to the config file. The question is the correct way to
    > get the config file in classes. There is only one config file and it is in
    > the root of the web site. I do not want to use a path since the location
    > of
    > the web site can change. The classes will be in the App_Code dir.
    >
    >
    >
    > Thank you,
    >
    > --
    > Jerry
    Steve, Oct 6, 2008
    #2
    1. Advertising

  3. Jerry C

    Jerry C Guest

    Steve,

    Thank you for you reply,
    I am not having any problems with the code. I was checking the community to
    see if I was using the correct code. This app started with framework 1 and I
    run into old code. Just checking for the latest.

    Thank you
    --
    Jerry


    "Steve" wrote:

    > Your cide looks correct to me. You are using the tilde to indicate the root
    > directory, which is always relative to the site, so should work wherever you
    > put the code. What problem are you having with this?
    >
    > Also, are you planning to read or write the web.config? Note that changes in
    > the web.config file will cause the Web server to restart, and will also
    > refresh the cache entries, which may cause problems in your application
    >
    > HTH
    >
    > "Jerry C" <> wrote in message
    > news:...
    > >
    > > I need to access the web.config file in a class. I am now using the line:
    > >
    > > rootWebConfig1 = WebConfigurationManager.OpenWebConfiguration("~")
    > >
    > > to get a reference to the config file. The question is the correct way to
    > > get the config file in classes. There is only one config file and it is in
    > > the root of the web site. I do not want to use a path since the location
    > > of
    > > the web site can change. The classes will be in the App_Code dir.
    > >
    > >
    > >
    > > Thank you,
    > >
    > > --
    > > Jerry

    >
    >
    >
    Jerry C, Oct 6, 2008
    #3
  4. Hi Jerry,

    Yes your code is correct.

    Regards,
    Allen Chen
    Microsoft Online Support
    --------------------
    | Thread-Topic: web.config in a class
    | thread-index: Ackn+DHlonxjN67AShKQCxnenpaCCQ==
    | From: =?Utf-8?B?SmVycnkgQw==?= <>
    | References: <>
    <>
    | Subject: Re: web.config in a class
    | Date: Mon, 6 Oct 2008 14:12:09 -0700
    | Lines: 46
    | Message-ID: <>
    | MIME-Version: 1.0
    | Content-Type: text/plain;
    | charset="Utf-8"
    | Content-Transfer-Encoding: 7bit
    | X-Newsreader: Microsoft CDO for Windows 2000
    | Content-Class: urn:content-classes:message
    | Importance: normal
    | Priority: normal
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.3119
    | Newsgroups: microsoft.public.dotnet.framework.aspnet
    | Path: TK2MSFTNGHUB02.phx.gbl
    | Xref: TK2MSFTNGHUB02.phx.gbl
    microsoft.public.dotnet.framework.aspnet:77402
    | NNTP-Posting-Host: tk2msftibfm01.phx.gbl 10.40.244.149
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    |
    | Steve,
    |
    | Thank you for you reply,
    | I am not having any problems with the code. I was checking the community
    to
    | see if I was using the correct code. This app started with framework 1
    and I
    | run into old code. Just checking for the latest.
    |
    | Thank you
    | --
    | Jerry
    |
    |
    | "Steve" wrote:
    |
    | > Your cide looks correct to me. You are using the tilde to indicate the
    root
    | > directory, which is always relative to the site, so should work
    wherever you
    | > put the code. What problem are you having with this?
    | >
    | > Also, are you planning to read or write the web.config? Note that
    changes in
    | > the web.config file will cause the Web server to restart, and will also
    | > refresh the cache entries, which may cause problems in your application
    | >
    | > HTH
    | >
    | > "Jerry C" <> wrote in message
    | > news:...
    | > >
    | > > I need to access the web.config file in a class. I am now using the
    line:
    | > >
    | > > rootWebConfig1 = WebConfigurationManager.OpenWebConfiguration("~")
    | > >
    | > > to get a reference to the config file. The question is the correct
    way to
    | > > get the config file in classes. There is only one config file and it
    is in
    | > > the root of the web site. I do not want to use a path since the
    location
    | > > of
    | > > the web site can change. The classes will be in the App_Code dir.
    | > >
    | > >
    | > >
    | > > Thank you,
    | > >
    | > > --
    | > > Jerry
    | >
    | >
    | >
    |
    Allen Chen [MSFT], Oct 7, 2008
    #4
  5. Jerry C

    Steve Guest

    Excellent. Working code is always easier to troubleshoot ;-)

    "Jerry C" <> wrote in message
    news:...
    > Steve,
    >
    > Thank you for you reply,
    > I am not having any problems with the code. I was checking the community
    > to
    > see if I was using the correct code. This app started with framework 1 and
    > I
    > run into old code. Just checking for the latest.
    >
    > Thank you
    > --
    > Jerry
    >
    >
    > "Steve" wrote:
    >
    >> Your cide looks correct to me. You are using the tilde to indicate the
    >> root
    >> directory, which is always relative to the site, so should work wherever
    >> you
    >> put the code. What problem are you having with this?
    >>
    >> Also, are you planning to read or write the web.config? Note that changes
    >> in
    >> the web.config file will cause the Web server to restart, and will also
    >> refresh the cache entries, which may cause problems in your application
    >>
    >> HTH
    >>
    >> "Jerry C" <> wrote in message
    >> news:...
    >> >
    >> > I need to access the web.config file in a class. I am now using the
    >> > line:
    >> >
    >> > rootWebConfig1 = WebConfigurationManager.OpenWebConfiguration("~")
    >> >
    >> > to get a reference to the config file. The question is the correct way
    >> > to
    >> > get the config file in classes. There is only one config file and it is
    >> > in
    >> > the root of the web site. I do not want to use a path since the
    >> > location
    >> > of
    >> > the web site can change. The classes will be in the App_Code dir.
    >> >
    >> >
    >> >
    >> > Thank you,
    >> >
    >> > --
    >> > Jerry

    >>
    >>
    >>
    Steve, Oct 7, 2008
    #5
  6. In my class library projects I'm using the following code to access the
    configuration file:

    string myValue =
    System.Configuration.ConfigurationManager.AppSettings.Get("myEntry");

    This code exists since the .NET 1.x times and still works. It has (IMO) one
    advantage over your solution, which is that it also works when the class
    library is used by a windows forms application.

    --
    Martin

    "Jerry C" wrote:

    >
    > I need to access the web.config file in a class. I am now using the line:
    >
    > rootWebConfig1 = WebConfigurationManager.OpenWebConfiguration("~")
    >
    > to get a reference to the config file. The question is the correct way to
    > get the config file in classes. There is only one config file and it is in
    > the root of the web site. I do not want to use a path since the location of
    > the web site can change. The classes will be in the App_Code dir.
    >
    >
    >
    > Thank you,
    >
    > --
    > Jerry
    Martin Bischoff, Oct 9, 2008
    #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?RGFuaWVs?=

    Machine.config & web.config

    =?Utf-8?B?RGFuaWVs?=, Jan 18, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    13,304
    Hermit Dave
    Jan 18, 2004
  2. Benny Ng
    Replies:
    9
    Views:
    9,954
    Benny Ng
    Oct 13, 2005
  3. E11
    Replies:
    1
    Views:
    4,742
    Thomas Weidenfeller
    Oct 12, 2005
  4. CSharpner
    Replies:
    0
    Views:
    1,017
    CSharpner
    Apr 9, 2007
  5. leeanne
    Replies:
    0
    Views:
    1,863
    leeanne
    Sep 24, 2008
Loading...

Share This Page