| Subcribe via RSS

Reading & Writing to a Cookie Inside a HttpHandler

October 13th, 2009 | Comments Off | Posted in VB.NET

When you are working within an HttpHandler, sometimes you need to access or write out state information. Since this is wrapped up in a class and not inheriting from the Page class, we need to approach this in a slightly different manner. The ProcessRequest method within IHttpHandler has a Response object drawn from HttpContext. The functionality works the same, we just need to adjust how we reference the cookie as follows:

Writing out a Cookie Reading from the Cookie

Reading Files from a Directory using C#

April 24th, 2009 | Comments Off | Posted in C#, General, HTML, IIS, Programming, VB.NET
So I had a project which I needed to find out what files were being held in a given directory and write those back to the browser. This is very helpful in automating posting of files to a website by your users without you being involved in tedious static html updates. The key here is the System.IO .NET class. We are going to access the DirectoryInfo class so we can iterate through and pull back all file objects contained within. In the code below, the DirectoryInfo looks in the current directory as we’ve defined through Server.MapPath(“.”). We could just have easily directed this through stepping up the directory (i.e. c:\intepub\wwwroot\directoryread\filestoread\). Next, we tell it to grab all the files that end with an .aspx extension which screens out any text or pdf files we may not be interested in. In the FileInfo class, we extract the name of each file to wire up the hyperlink and print this back to the user. I’ve taken it one step further to strip out the hyphens and the .aspx file extension that we print back to the user so the file name “Reading-files-from-a-directory-using-csharp.aspx” becomes “Reading files from a directory using cSharp”. Much cleaner and user friendly. That FileInfo class has lots of cool properties including LastWriteTime, CreationTime, Length (for a full rundown on the FileInfo properties checkout Microsoft’s .NET Framework Developer Center). Tags: , ,

Accessing Session Variables from a Class

February 27th, 2009 | Comments Off | Posted in VB.NET

I’ve got a vb.net class file that needed to access the Session object to determine if the logged in user qualified for special product pricing. Session(“user_group”) spits back a nice nasty little message saying “Name ‘Session’ is not declared.” The compiler is right. Its not declared. Since we are working within the bounds of the class, there is no System.Web space by default like the Page object enjoys so to pull back the Session content we simply need to fully qualify where the compiler can find that Session object:

System.Web.HttpContext.Current.Session(“networkname”)

Tags: , ,