Web.Config Config Config



  • Ahh the Web.Config file. Glorious!. How do you read this wonderful thing?

    XmlDocument x = new XmlDocument();
    x.Load(HttpContext.Current.Request.PhysicalApplicationPath + "web.config");
    XmlNode node = x.SelectSingleNode("/configuration/system.web/authentication/forms");
    if (node != null && node.Attributes["timeout"] != null && !String.IsNullOrEmpty(node.Attributes["timeout"].Value))
       _sessionTimeout = Convert.ToInt32(node.Attributes["timeout"].Value);
    


  • You'll have to explain TRWTF to me, I'm afraid. Is it that a moderately nested XML configuration file is used to store only one value?



  • @Faxmachinen said:

    You'll have to explain TRWTF to me, I'm afraid. Is it that a moderately nested XML configuration file is used to store only one value?

    No, Web.Config stores a buttload more. However, OP could have got the same value like so:

    TimeSpan _sessionTimeout = FormsAuthentication.Timeout


  • @ShaggyB said:

    Ahh the Web.Config file. Glorious!. How do you read this wonderful thing?

    XmlDocument x = new XmlDocument();
    x.Load(HttpContext.Current.Request.PhysicalApplicationPath + "web.config");
    XmlNode node = x.SelectSingleNode("/configuration/system.web/authentication/forms");
    if (node != null && node.Attributes["timeout"] != null && !String.IsNullOrEmpty(node.Attributes["timeout"].Value))
       _sessionTimeout = Convert.ToInt32(node.Attributes["timeout"].Value);
    

    Fucking web.configs, how do they work?



  • jedi suggestive hand sweep These are not the attributes you're looking for.



  •  My wheel has corners.

    It's a bonus feature!



  • @Kyanar said:

    No, Web.Config stores a buttload more. However, OP could have got the same value like so:

    TimeSpan _sessionTimeout = FormsAuthentication.Timeout

    Even ignoring this, they're ought to be a more readable way to do...

    XmlNode node = x.SelectSingleNode("/configuration/system.web/authentication/forms");
    

    if (node != null && node.Attributes["timeout"] != null && !String.IsNullOrEmpty(node.Attributes["timeout"].Value))

    ...no? I can't be bothered to read XmlNode.SelectSingleNode's documentation, but it probably can return attributes too.



  • If you absolutely must programatically re-read the web.config file's values almost every single one is accessible in a much more straightforward fashion, as demonstrated by Kyanar.  Invoking an xml reader is defeating the purpose of web.config.

    What are you trying to accomplish here?  Just open the dang thing up in notepad and read the value and hard code it in the application -- unless you're mucking about with the timeout value all the time.  Besides, the timeout value is over-ridable by IIS and as such the value in web.config may be a red herring.

     



  • @Shortjob said:

    jedi suggestive hand sweep These are not the attributes you're looking for.

    Someone's been reading Not Always Right.



  • @AngelSL said:

    @Shortjob said:
    jedi suggestive hand sweep These are not the attributes you're looking for.

    Someone's been reading Not Always Right.

    Yah, but isn't Shortjob from Indiana Jones? Wrong Harrison Ford movie! (Or was he Short Round?)



  • @Zecc said:

    Even ignoring this, they're ought to be a more readable way to do...

    XmlNode node = x.SelectSingleNode("/configuration/system.web/authentication/forms");
    if (node != null && node.Attributes["timeout"] != null && !String.IsNullOrEmpty(node.Attributes["timeout"].Value))

    ...no? I can't be bothered to read XmlNode.SelectSingleNode's documentation, but it probably can return attributes too.

     

    Edit: Well, nice to know that the .NET interfaces to XPath are just as inconvenient as the Java interfaces. You can get better than the above, but you'd still have something like:

    _sessionTimeout = x.CreateNavigator.Evaluate("Number(/configuration/system.web/authentication/forms/@timeout)"); 

    This will throw an exception if @timeout doesn't exist or is not a number, but that is pretty much what the OP would have done anyway I think.

     


  • @PSWorx said:

    @Zecc said:

    Even ignoring this, they're ought to be a more readable way to do...

    XmlNode node = x.SelectSingleNode("/configuration/system.web/authentication/forms");
    if (node != null && node.Attributes["timeout"] != null && !String.IsNullOrEmpty(node.Attributes["timeout"].Value))

    ...no? I can't be bothered to read XmlNode.SelectSingleNode's documentation, but it probably can return attributes too.

     

    Not that it would matter, but yes. Apparently SelectSingleNode takes arbitrary XPath expressions, so you could just do:

    _sessionTimeout = x.SelectSingleNode("/configuration/system.web/authentication/forms/@timeout").Value; 

    Assuming that the attribute in question exists. But then again, you'd want to handle the other case in an exception anyway, would you?

     

    ... or you could just read the already-parsed version ASP.net keeps around. Which also takes into account any overrides that may be present in the IIS configuration.

    Go ahead and discuss the wrong solution, I just want to make sure that the correct solution is here in case any programmers new to ASP.net come in.



  • @blakeyrat said:

    Go ahead and discuss the wrong solution, I just want to make sure that the correct solution is here in case any programmers new to ASP.net come in.

     

    But discussion the wrong solution is half the fun of TDWTF. Besides, there were two different questions here:

    1.: Is there a concise way to solve the OP's proble? - Yes, use the pre-parsed property values

    2.: Is there generally a better way to parse XML configs with .NET or do you have to employ the same WTFery the OP did? - Which is the question Zecc and me tried to solve.

    (Incidentally, sorry for editing the post above after I've been replied to. I didn't realize there was a reply already, no bad intentions. It was just that the original version wouldn't have worked anyway)



  • @Zecc said:

    @Kyanar said:

    No, Web.Config stores a buttload more. However, OP could have got the same value like so:

    TimeSpan _sessionTimeout = FormsAuthentication.Timeout

    Even ignoring this...
    Don't ignore it.  Reading Web.config directly is a bad idea.  ASP.Net has a whole config inheritance hierarchy that you would be breaking.  You're also tying yourself to the physical implementation of Web.config, so it will be more likely to break in future versions.  Also, your original complaint was entirely due to your ignorance of the XPath "@" axis shortcut and has nothing to do with a shortcoming of the .Net XML library.



  • I apologize for writing "they're" instead of "there", by the way.



  • @Kyanar said:

    No, Web.Config stores a buttload more. However, OP could have got the same value like so:

    TimeSpan _sessionTimeout = FormsAuthentication.Timeout

    Is there a way to get it via the ConfigurationManager class? I know ConfigurationManager.AppSettings can be used to get stuff in <appSettings>



  • @Zecc said:

    I apologize for writing "they're" instead of "there", by the way.

    Apology excepted.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.