Q: I’m using the default web site project model in Visual Studio 2005. When I add a global.asax file to my web site, it wants me to use inline code. What do I do to get a code-behind file?
A: First, create a class that derives from HttpApplication. You'll want to override the Init event in this class. This class can live in a separate class library project, or in the App_Code directory. The class might look something like this:
Next, add an Inherits attribute to global.asax, and point the attribute to the class we’ve just created.
You now have code in a real code file.
Before we close the post out, let me offer a word of advice:
Don’t let your HttpApplication derived class become cluttered up with a huge mess of unrelated code. Be a minimalist with global.asax code. Also, read Karl Seguin’s article “Global.asax? Use HttpModules Instead!”. HttpModules are reusable and configurable, and should be the preferred mechanism for processing most application events.
Q: I'm having problems setting meta tags in my @ Page directive like in your article. My graphics designers don't understand code, but they can deal with page directives. It would be great to have this approach working. Do you have an example I can download?
A: Download the example web project.
The project contains four web forms, one class file, and one master page. The class file defines a base class for all web forms, and looks like the following.
The article was demonstrating how to use a base class from a web form with inline code. We need a slightly different approach for web forms using code-beside.
This project has four combinations:
To set the MetaKeywords property from the @ Page directive with code-beside, you'll need the following:
Notice the use of the CodeFileBaseClass attribute, which is needed for ASP.NET to pick up the Metakeywords property at the time it generates code. The other required step is to make sure the class in the code-beside file derives from the base class we defined.
You'll still have red squiggly lines in the @ Page directive. Visual Studio is overly zealous about validating the @ Page directive, and won't like the Metakeywords attribute. This is just a validation error and doesn't prevent the web form from compiling and executing. ASP.NET will generate the right code the words inside the MetaKeywords attribute will render into the HTML output.
This approach isn't limited to just meta tags, we could give the base class other public properties that we can set in the @ Page directive and use at runtime.
Hope that helps...
Q: In my application, I am using some static fields to hold information. How do I make sure the garbage collector doesn't collect the objects referenced by the static fields? How long will the objects stay in memory?
A: A static (C#) or shared (VB) field establishes a root reference. The garbage collector will only collect objects that can't be reached directly or indirectly from a root reference. The objects you reference from static fields are safe from garbage collection.
Assuming a static field always references the same object, the object will be around for the duration of the application (for the duration of the application domain, to be precise). You'll want to be careful referencing extremely large objects with static fields, because they'll be around for a long, long time. In a web app you might consider using the ASP.NET cache instead.
The biggest concern to have with static and shared fields is thread safety. If you have multiple threads running and are modifying the objects, you'll have to make sure to do so in a thread safe way. See Statics and Thread Safety Part One and Two.
Q: I'm having a problem with performance in my ASP.NET master page. In the debugger I see the master page runs during each postback. How do we stop the master page from running its Load event each time?
A: The MasterPage class derives from UserControl, which gives a hint as to why the master behaves this way. The master page becomes a control inside the page, and like every control in ASP.NET the master recreates itself on every postback (see Master Pages: Tips, Tricks, and Traps).
You might be able to improve the situation using some common ASP.NET performance tips:
A real programmer on the Win-OT list has been waxing eloquently about apt-get. He does have a point.
apt-get is one of the command line tools that can administer software packages on Linux distros. apt-get manages all the gnarled dependencies and makes installing new software on Linux mostly a breeze. Throw on one of the GUI front ends for apt-get, and new software is only a few clicks away. Want to program in Ruby? Click - click. Want a web server? Click - click. It's cool (and by cool I mean totally sweet).
The equivalent to apt-get on a Windows desktop is, I guess, a combination of "Add / Remove Programs" and "Windows Update". Sorry … "Microsoft Update". I have both of these icons on my Start menu and I forgot what the difference is, really, but the Microsoft Update icon is the prettiest of the two, so it must be better. I'm shallow that way.
"Add Remove Programs" doesn't really add any software to a machine, unless the software is an operating system component and you have the CD. I mean, you really have the CD close enough to insert into the CD drive. Heaven help you if it's 1 AM at the hotel room in Dog Lick, Kentucky and you need to install MSMQ onto your genuine, activated copy of Windows. Thank goodness for ISO images.
I've never seen anything interesting appear on Microsoft update. Stuff like the Consolas font or Power Toys. On Tuesday, however, two of my machines installed a critical update to remove unacceptable symbols from a font. I thought I'd sleep better that night knowing these foul symbols were gone, but I had strange dreams about super-intelligent aliens covertly landing on earth, graduating from the finest law schools, and then enslaving the planet using a loophole in international maritime laws. Related? Yes.
It's painful to move to a new machine that doesn't have the software I need. Search Microsoft.com, page through results, download, save, open, click-click-click-click, and install. Why aren't the pastel sunsets of the Nature Theme not available through Microsoft Update? Why doesn't the Web Application Project add-on appear? Will I forever need to download and install PowerShell manually?
I've been kicking around the idea of building an app or script to download and install the common software I use, but some of this software requires registration, and other software requires passing the Windows Genuine Advantage test. It might be an interesting challenge. If Microsoft provided a tool that could pick from the smorgasbord of add-ons, utilities, and applications spread across the Microsoft.com domain, it would be cool (and by cool, I mean totally sweet).
Downloads, downloads every where,
and I can't find the links;
Downloads, downloads every where,
I just want machines in synch.
Apologies to Samuel T.
Q: I want to programmatically retrieve a web page and parse out some information inside. What's the best approach?
A: For fetching the contents of a page, the simplest class to use is the System.Net.WebClient class. For more advanced scenarios that require cookies and simulated postbacks to the server, chances are you'll have to graduate to the System.Net.WebRequest and WebResponse classes. You'll find a lot of material on the web that demonstrate how to use these classes.
If you have to pull specific information out of a page, then the "best approach" will depend on the complexity of the page and nuances of the data. Once you have the contents of a page in a string variable, a few IndexOf() and Substring() method calls might be enough to parse out the data you need.
Many people use the RegEx class to find data inside of HTML. I'm not a fan of this approach, though. There are so many edge cases to contend with in HTML that the regular expressions grow hideously complex, and the regular expression language is notorious for being a "write-only" language.
My usual approach is to transform the web page into an object model. This sounds complicated, but not if someone else does all the heavy lifting. Two pieces of software that can help are the SgmlReader on GotDotNet, and Simon Mourier's Html Agility Pack. The agility pack is still a .NET 1.1 project, but I have it running under 2.0 with only minor changes (I just needed to remove some conditional debugging attributes). With these libraries, it is easy to walk through the page like an XML document, perform XSL transformations, or find data using XPath expressions (which to me are a lot more readable than regular expressions).
Here is a little snippet of code that uses the agility pack and will dump the text inside all of the links (<a> anchor tags) on OTC's front page.
The "Scraping" term in the title of this post comes from "screen scraping", which is a term almost as old as IT itself.
It's a quiet day.
Too quiet - you think to yourself.
Then it happens….
A message arrives in your Inbox, but not just any message.
This message is adorned with the Red Exclamation of Importance!
You can feel the adrenaline in your fingers as they snap the mouse into position. A breathless moment during the double-click, and the message is now in front of your eyes:
Hi everyone! Just a little reminder to get those TPS status reports in by next week. Also, we've change the passcode for next month's conference call – it is now 6661212.
Thanks! :)
Oh. It's from that person. Every mail that person sends carries the Red Exclamation of Importance.
Have you ever wondered what that person would do in the event of a real emergency?