August 1, 2009

Solid relaxing day!

Weekends seem to be good times to relax, yet the last few have been busy for me. Not today though! I had a nice day running and relaxing by a pool. I wish every day was like this!

July 31, 2009

HTTP Status Codes Class

One of my apps required to graph and chart IIS logs. I of course was using logparser to do this and everything seemed to be working out. The request changed on me slightly so that I can define what the status code is as opposed to just saying the number of 404's or 200's. Here is an easy Dictionary class that you can use:
public class HttpStatusCodes
{
static Dictionary _map = ConstructStatusCodesMap();

public static string GetDescription(int statusCode)
{
string description = "";
if (_map.ContainsKey(statusCode))
description = _map[statusCode];

return string.Format("{0} - {1}", statusCode, description);
}

private static Dictionary ConstructStatusCodesMap()
{
Dictionary map = new Dictionary();

// http://status-code.com/
#region Construct HTTP status codes map

map.Add(100, "Continue");
map.Add(101, "Switching Protocols");
map.Add(102, "Processing");
map.Add(200, "OK");
map.Add(201, "Created");
map.Add(202, "Accepted");
map.Add(203, "Non-Authoritative Information");
map.Add(204, "No Content");
map.Add(205, "Reset Content");
map.Add(206, "Partial Content");
map.Add(207, "Multi-Status");
map.Add(226, "IM Used");
map.Add(300, "Multiple Choices");
map.Add(301, "Moved Permanently");
map.Add(302, "Found");
map.Add(303, "See Other");
map.Add(304, "Not Modified");
map.Add(305, "Use Proxy");
map.Add(306, "(Unused)");
map.Add(307, "Temporary Redirect");
map.Add(400, "Bad Request");
map.Add(401, "Unauthorized");
map.Add(402, "Payment Required");
map.Add(403, "Forbidden");
map.Add(404, "Not Found");
map.Add(405, "Method Not Allowed");
map.Add(406, "Not Acceptable");
map.Add(407, "Proxy Authentication Required");
map.Add(408, "Request Timeout");
map.Add(409, "Conflict");
map.Add(410, "Gone");
map.Add(411, "Length Required");
map.Add(412, "Precondition Failed");
map.Add(413, "Request Entity Too Large");
map.Add(414, "Request-URI Too Long");
map.Add(415, "Unsupported Media Type");
map.Add(416, "Requested Range Not Satisfiable");
map.Add(417, "Expectation Failed");
map.Add(418, "I'm a teapot");
map.Add(422, "Unprocessable Entity");
map.Add(423, "Locked");
map.Add(424, "Failed Dependency");
map.Add(425, "(Unordered Collection)");
map.Add(426, "Upgrade Required");
map.Add(500, "Internal Server Error");
map.Add(501, "Not Implemented");
map.Add(502, "Bad Gateway");
map.Add(503, "Service Unavailable");
map.Add(504, "Gateway Timeout");
map.Add(505, "HTTP Version Not Supported");
map.Add(506, "Variant Also Negotiates");
map.Add(507, "Insufficient Storage");
map.Add(510, "Not Extended");

#endregion

return map;
}
}
Using it would be something like:
 return HttpStatusCodes.GetDescription((int)value);

July 30, 2009

ASP.NET MVC Install on IIS6

OK, the fun part of MVC is installing it on IIS6. You can find a bunch of solutions if you google for it, but I wanted to get the two that I tried here on my blog.
  1. The easy option is to use a wildcard mapping for aspnet_isapi.dll. You simply map the “.mvc” extension on this new virtual directory and ensure the “Verify file exists” setting is turned off. This tells IIS 6 to process all requests using ASP.NET, so routing is always invoked.

  2. Second option is to use a url-rewriter. A free one can be found here. Download and install and then set up the httpd.ini file like this:
[ISAPI_Rewrite]

# Defend your computer from some worm attacks
RewriteRule .*(?:global.asa|default\.ida|root\.exe|\.\.).* . [F,I,O]

# -----------------------------
# http://blog.codeville.net/2008/07/04/options-for-deploying-aspnet-mvc-to-iis-6/
# -----------------------------
# If you're hosting in a virtual directory, enable these lines,
# entering the path of your virtual directory.
UriMatchPrefix /MySite
UriFormatPrefix /MySite

# Add extensions to this rule to avoid them being processed by ASP.NET
RewriteRule (.*)\.(css|gif|png|jpeg|jpg|js|zip) $1.$2 [I,L]

# Normalizes the homepage URL to /
RewriteRule / /default.aspx [I]

# Prefixes URLs with "rewritten.aspx/", so that ASP.NET handles them
RewriteRule /(.*) /rewritten.aspx/$1/ [I]
# -----------------------------
# End of solution
# -----------------------------
You may need to play with the rewrite ... I know I had to for it to work for me. In the end I chose the rewrite option since I did not want the .mvc extension.

July 29, 2009

Powershell Perfmon

Here is how you can access performance counters using Powershell.

July 28, 2009

JQuery Captcha

Captcha has always been an interesting thing. I understand the need for it, yet as bots get smarter the captcha programs also try and get smarter. Unfortunately, that leads us to a very hard to read (even for a human) image. ReCaptcha has a .NET API which I have used, but even ReCaptcha is getting hard to read. I thought about doing a math equation captcha (kind of like the goggles that gmail has), but I wanted something nice. I came across this really cool idea! It needs some work as it does not work in all browsers (chrome) and it also probably needs to randomize ids somehow.

The idea though is very cool :)

July 27, 2009

Scheduled Tasks using Powershell

How would one go about getting scheduled tasks and other related information to tasks on your machine using Powershell. This was the question asked to me recently. I know there has to be a better way, but here is one way of doing it:
function DumpFolders($folder)
{
$folder

$folder.GetFolders(0) | % {
DumpFolders $_
}
}

# MSDN:
# Pass in 1 to return all running tasks, including hidden tasks.
# Pass in 0 to return a collection of running tasks that are not hidden tasks.
$visibilityFlag = 1

$svc = new-object -com Schedule.Service

$svc.Connect()

Write-Host "-------------------------------"
Write-Host " Folders"
Write-Host "-------------------------------"
DumpFolders $svc.getfolder("\")

$folder = $svc.getfolder("\")

Write-Host "-------------------------------"
Write-Host " Tasks"
Write-Host "-------------------------------"
$folder.GetTasks($visibilityFlag) | select name, path, enabled, lastruntime, nextruntime

Write-Host "-------------------------------"
Write-Host " All Tasks"
Write-Host "-------------------------------"

function DumpFolderTasks($folder)
{
$tasks = $folder.GetTasks($visibilityFlag)
$tasks | % {
@("", "Name: $($_.Name)", "Enabled: $($_.Enabled)", "LastRunTime: $($_.LastRunTime)")
}

$folder.GetFolders(0) | % {
DumpFolderTasks $_
}
}

DumpFolderTasks $svc.getfolder("\")


Anyone have a good way of doing this?

July 26, 2009

MVC and .NET

I have extensively using the MVC pattern lately using the framework provided by Microsoft. I'll start blogging a bit about it this week if I get some time.