IIS Handler for Custom Url

Recently need to setup environment for bug fix.

The client side code calling url like http://somewhere/upload/, while at the server side the url need to receives the POST request from client to perform certain actions.

We have the ready client code to send the POST request with the contents, the missing part is the receiving side, the url in server. Current code will always give us error whenever calls made to the url because the url does not exists at all. My mission today will be faking a url to receives request and output the content as a new file, so we can test the bug fix later on.

Client code: (Refer to Posting multipart/form-data Using .NET WebRequest)

private string ExecutePostRequest(
Uri url,
Dictionary<string, string> postData,
string fileMimeType
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.AbsoluteUri);
request.Method = "POST";
request.KeepAlive = false;
string boundary = "ILikeSeperator";
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.Credentials = CredentialCache.DefaultCredentials;

// Connect to server and start sending data (use POST)
Stream requestStream = request.GetRequestStream();
postData.WriteMultipartFormData(requestStream, boundary);

byte[] endBytes = System.Text.Encoding.UTF8.GetBytes("--" + boundary + "--");
requestStream.Write(endBytes, 0, endBytes.Length);

// Get back response from server
using (WebResponse response = request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
return reader.ReadToEnd();

Nothing much to say about the client code 😀

Server Code: (My works here)

The url I going to creates is http://localhost/uploadTest. We need to add a ashx handler in IIS to handle the calls into this url. The handler will create a new text file base on contents sent by client.

There are total 6 steps needed and the sequence numbers will be highlight in Bold + red colour = Number

~~~ 0 ~~~ 0 ~~~

1- First follow the must read step-by-step guides on creating handler for specific url – ASP.NET ASHX Handler

Project created follow above guide, removed unnecessary .aspx and files.

Aspx Handler Project

2- Another important step, use correct Web.config:-

<?xml version="1.0"?>
For more information on how to configure your ASP.NET application, please visit
<customErrors mode="Off"/>
<urlMappings enabled="true">
<add url="~/Default.aspx" mappedUrl="~/FileUploadHandler.ashx"/>
<compilation debug="true"/>
<remove name="WebDAVModule"/>
<handlers accessPolicy="Read, Write, Execute, Script">
<remove name="StaticFile"/>
<remove name="SimpleHandlerFactory-ISAPI-2.0"/>
<remove name="WebDAV"/>
<remove name="SimpleHandlerFactory-Integrated-4.0"/>
<remove name="SimpleHandlerFactory-Integrated"/>
<add name="SimpleHandlerFactory-Integrated" path="*.ashx" verb="GET,HEAD,POST,DEBUG,PUT,DELETE" type="System.Web.UI.SimpleHandlerFactory" resourceType="Unspecified" requireAccess="Write" preCondition="integratedMode"/>
<add name="SimpleHandlerFactory-Integrated-4.0" path="*.ashx" verb="GET,HEAD,POST,DEBUG,PUT,DELETE" type="System.Web.UI.SimpleHandlerFactory" resourceType="Unspecified" requireAccess="Write" preCondition="integratedMode,runtimeVersionv4.0"/>
<add name="SimpleHandlerFactory-ISAPI-2.0" path="*.ashx" verb="GET,HEAD,POST,DEBUG,PUT,DELETE" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv2.0,bitness32" responseBufferLimit="0"/>
<add name="StaticFile" path="*" verb="GET,HEAD,POST,DEBUG,PUT,DELETE" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" resourceType="Either" requireAccess="Read"/>
<remove users="*" roles="" verbs=""/>
<add accessType="Allow" users="*" verbs="GET,HEAD,POST,PUT,DELETE,DEBUG"/>

3- The core of the handler code inside FileUploadHandler.ashx:

public void ProcessRequest(HttpContext context)
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");

// Read input
string requestContent = new StreamReader(context.Request.InputStream).ReadToEnd();
if (string.IsNullOrWhiteSpace(requestContent))

string[] requestArray = requestContent.Split(new[] { "--ILikeSeperator" }, StringSplitOptions.RemoveEmptyEntries);

if (requestArray.Count() > 0)
string fileContent = GetContent(requestArray[0]);

if (!string.IsNullOrWhiteSpace(fileContent))
// Send back response to client

// Create upload file

4 – IIS Setup: Add new application, use default authentication

Ashx IIS Setup

5 – Deploy the Ashx handler to upload directory (Created from AspxHandler project).

Remember use Web.config given above.


6 – Set folder permission in uploadTest directory. Allow write permission for IIS_IUSRS (This is built-in IIS user) so handler able to create new folder and files.


You will get errors in client and server if no write permission is allow in mapped directory of the url:

Client error:

System.Net.WebException was unhandled
  Message=The remote server returned an error: (500) Internal Server Error.

Error in client code

Server Error:

System.UnauthorizedAccessException was unhandled by user code
  Message=Access to the path ‘C:\inetpub\wwwroot\uploadTest\MyUploadContents\’ is denied.

Error in Server code


Enjoy your customize handler just for the url you want!

Next post will talks about the debugging in ashx handler, a simple and short article 😉

Sample source code:



Customize url handler, file upload handler, file upload ashx, iis handler, HttpWebRequest


WPF Performance – General

Deal with Performance in WPF applications

This article tells the basic thread structure of WPF (which I thinks is important for me right now as I need to know that for the WPF performance tuning), some tricks and basic concepts of WPF design.

Good article (I said that because probably I can understand them now after some time :P, also if the article can explain in more simpler way when compare to Microsoft documents)

Try to follow WPF design concept to have a best performance (MVVM, Display UI on the fly,…)

Next post will be some technical findings when I works on WPF performance.

Prism RegionContext become NULL when View Removed from Region

Found a bug in Prism Region Context last month and Prism team has quick fix for this that work flawless!

Thread posted: Region context set to NULL when remove any view in region

Solution : Prism v4: RegionContext lost when removing a view from a region

You can find the sample project in my thread, looks for the name “Prism_RegionContext”, and below shows the key part captured, read the comments:

Search keywords : Prism v4.0, context, region, Communicating between components, MVVM

Reformat/Re-align code in Visual Studio

Found a useful tip when I looking for StyleCop essay.

How to: Reformat your code in Visual Studio

In brief, do these:

  1. Select the code you want to re-format (CTRL + A if want to re-format whole file).
  2. Press CTRL + K
  3. Press CTRL + F
  4. Done.


If work in VB.Net, it will auto set TitleCase and align the code, however in C#, it only re-align the code, but this is the feature I like the most since C# code never align for you again if the alignment was out.


Reference: How to: Reformat your code in Visual Studio

Keyword: Alignment, align, code style

Add New Field in MS Access Programmatically using ADOX (C#.Net)

You need to add reference before you start (Refer to my previous post – Rename Field in MS Access Programmatically using ADOX (C#.NET) )

using ADOX;

// Skipped namespace and public class here...

private void btnUpdate_Click(object sender, EventArgs e)
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Northwind.mdb;Persist Security Info=False";
   CatalogClass cat = new CatalogClass();


   AddField(cat, "TargetTable", "Employee_Name_2", DataTypeEnum.adVarWChar, 50, string.Empty);

private void AddField(ADOX.CatalogClass cat, string tableName, string newFieldName, DataTypeEnum varType, int size, string defaultValue)
   // #1
   // cat.Tables[tableName].Columns.Append(newFieldName, varType, size);

   ColumnClass col = new ColumnClass();
   col.Name = newFieldName;
   col.Type = varType;
   col.DefinedSize = size;

   // Make this field become nullable field.
   col.Attributes = ColumnAttributesEnum.adColNullable;

   // #2
   cat.Tables[tableName].Columns.Append((object)col, DataTypeEnum.adInteger, 0);
   // #3 - Assign default value after column appended.
   if (!string.IsNullOrEmpty(defaultValue))
      col.Properties["Default"].Value = defaultValue;

Issues when using Append() method

Issues when using Append() method

Issues when using Append() method (Refer #1 in source code above):

  1. You only can defines field name, variable type and size for the new field.
  2. New field are not allow NULL when created. (Tested in Access 2003).
  3. You cannot sets default value for new field.

In order to create nullable column with default value,  I use a ColumnClass object – col, and set the necessary properties.

Few things here:

  1. From reference #2, col object need to pass as object type in Append(…) method.
  2. When you pass col object into Append() method, the rest of the arguments in Append(…) actually do NOT overwrite the ‘Type’ and ‘DefinedSize’ properties in col object. So the 2nd argument and 3rd argument is just to fill up the extra places in Append(…).
  3. In reference #3, you only can assign the default value after appended the new field. (Refer to FIX: Cannot Use ADOX to Set Default Value for Column Property)

~~~ [ End ] ~~~


  1. Rename Field in MS Access Programmatically using ADOX (C#.NET)
  2. FIX: Cannot Use ADOX to Set Default Value for Column Property

Keyword: ALTER COLUMNS, ALTER TABLE, Microsoft Access

Rename Field in MS Access Programmatically using ADOX (C#.NET)

MS Access is a lightweight database and lack of some database feature, for example it does not support the SQL query to rename the field. Therefore we need to workaround to rename the field in MS Access.

MS Access SQL reference: Microsoft Office Access – SQL Reference

Instead I never had VB6 in my PC but only .NET, so I looking for workaround in .NET. Below are the references:

  1. ADO.NET Programmer’s Reference – Chapter 16 – COM Interoperability (Just search for ‘ADOX’ in this article to check for ADOX usage).
  2. How to rename the column name in MS Access table (Reference in VB6, Main source for me to convert ADOX into .NET)

You need to add the ADOX COM reference into your .NET project because we are going to use ADOX to do the trick.

Add ADOX COM Reference from Solution Explorer

Add ADOX COM Reference from Solution Explorer

First you need to to go to Add Reference from Solution Explorer, add Microsoft ADO Ext.2.8 for DDL and Security from COM tab.

Then, use the code below,

using ADOX;

// Skipped namespace and public class here...

private void btnUpdate_Click(object sender, EventArgs e)
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Northwind.mdb;Persist Security Info=False";
   CatalogClass cat = new CatalogClass();
   // cat.ActiveConnection = connString    <-- Error here
   // Cannot assign to cat.ActiveConnection directly.
   // Use code at below to assign connection string in C#.


RenameField(cat, "TargetTable", "Staff", "Employee");


private void RenameField(CatalogClass cat, string tableName, string originalFieldName, string newFieldName)
    cat.Tables[tableName].Columns[originalFieldName].Name = newFieldName;

Run the button btnUpdate_Click to make the trick work, so the field Staff will rename to Employee.

Note that you cannot assign the connection string directly to cat.ActiveConnection since it is not a string object, and I guess it only accept Connection object from ADO. Use cat.let_ActiveConnection(connString) instead.

Micorsoft example on ADOX connection string – PRB: Unhandled Exception When You Set ADO Property to a String in Visual C# .NET

~~~ End ~~~

Upcoming article: Add New Field in MS Access Programmatically using ADOX (C#.Net)


  1. Microsoft Office Access – SQL Reference
  2. ADO.NET Programmer’s Reference – Chapter 16 – COM Interoperability
  3. How to rename the column name in MS Access table
  4. http://support.microsoft.com/kb/309047

Keyword:  ALTER COLUMNS, ALTER TABLE, Microsoft Access

TryParse – out parameter cannot work with Property

Today I facing the exact situation written in reference #1, I tried to pass the property to TryParse out parameter.

Below is the sample code:

int i;

public int I

int.TryParse(str, out I);   // pass I (not i) as out parameter

But I got this error message:

A property or indexer may not be passed as an out or ref parameter.

Till now I cannot get any satisfactory explanation about this error, however I think the most acceptable answer for now is from reference #2.

The fact that a property looks and feels like a variable is deceiving. When compiled, a property becomes two methods: one for the Getter and one of the Setter. This is why the property fails when you try to use it in TryParse().

Solution is simple, just place the original variable as out parameter.

This question make me need to understand MSIL one day to find out what happen inside there…


  1. Mano’s Tech Blog – Different ways to convert string to an integer in C#
  2. Why TryParse Can’t Get Property?

Keyword: Accessor, get,set,getter,setter,error