HTTP Redirect with non-ASCII characters in the URL

Another day, another small learning.

SITUATION

I have been extending ASP.NET Core REST API service that returns redirect (HTTP 301) results for a query. I would submit something like /redirect/A10293 and it would look up the associated SharePoint library URL for that entity.

/redirect/A10293 would then be redirected to https://sharepoint/site/library.

The customer wanted to add another piece of input, to be passed as a parameter of type string: the description of the entity. In SharePoint I had made a SPFx application customizer extension that showed this information in the header.

The new flow would be:

/redirect/A10293?name=Hello%20World should return a redirect to https://sharepoint/site/library?name=Hello%20World.

SYMPTOMS

In testing, we found out that for some entity names the service would return a Server error (HTTP 500). In particular, for the names with strange characters in the name, such as German umlauts like Müller.

/redirect/A10293?name=Hello%20Herr%20M%C3%BCller would fail with a 500.

CAUSE

Thanks to my colleague Christoph König (yes, with umlaut in the surname).

The cause of the error is that HTTP redirects are restricted to ASCII characters in the redirect result, as per the RFC.

I (non readable character) non-ascii characters
I do, really. Especially my surname last character: ć. Thanks Unicode!

In the code I was constructing a string and adding the name parameter as a string. It was being automatically URL-decoded by ASP.NET, and the redirection URL was then something like https://sharepoint/site/library?name=Hello Herr Müller. This tripped the redirect ActionResult parser to raise an exception.

SOLUTION

Use the System.Uri class to parse the string and return its AbsoluteUri property. A string, with the canonical, encoded version safe to be used in redirect result.

string redirectUrl = " https://sharepoint/site/library?name=Hello Herr Müller ";
Uri redirectURI = new Uri(redirectUrl);
return RedirectPermanent(redirectURI.AbsoluteUri);

ASP.NET Unit Testing With Typemock Isolator

I’ve just stumbled upon this extraordinary offer. I’ve been evaluation Typemock before and it’s one of the best (if not the best) unit testing tool for SharePoint. And now its creators are launching a wide promotion campaign:

Unit Testing ASP.NET? ASP.NET unit testing has never been this easy.

Typemock is launching a new product for ASP.NET developers – the ASP.NET Bundle – and for the launch will be giving out FREE licenses to bloggers and their readers.

The ASP.NET Bundle is the ultimate ASP.NET unit testing solution, and offers both Typemock Isolator, a unit test tool and Ivonna, the Isolator add-on for ASP.NET unit testing, for a bargain price.

Typemock Isolator is a leading .NET unit testing tool (C# and VB.NET) for many ‘hard to test’ technologies such as SharePoint, ASP.NET, MVC, WCF, WPF, Silverlight and more. Note that for unit testing Silverlight there is an open source Isolator add-on called SilverUnit.
The first 60 bloggers who will blog this text in their blog and tell us about it, will get a Free Isolator ASP.NET Bundle license (Typemock Isolator + Ivonna). If you post this in an ASP.NET dedicated blog, you’ll get a license automatically (even if more than 60 submit) during the first week of this announcement.

Also 8 bloggers will get an additional 2 licenses (each) to give away to their readers / friends.
Go ahead, click the following link for more information on how to get your free license.

Windows Defender Update and the Page Error in Development Web Server

Another of the strange, unexplained errors with a simple (yet unexpected) cause.

SYMPTOMS

You are developing an ASP.NET Site or Web Project in Visual Studio 2005/2008. You start the Debug process. Your browser window opens with a message:

Page Load Error

or

The page cannot be found.

Additionally, you have Windows Defender installed on your machine.

THE CAUSE

A flawed update of the Windows Defender removes the localhost entries from hosts file in windowssystem32devicesetc folder.

SOLUTION

Manually add the following entries back to the hosts file:

127.0.0.1 localhost

More details: http://www.h-online.com/security/Windows-Defender-False-alarm-triggered-by-hosts-file–/news/112814

.NET Framework 3.5 SP1 and Training Update

As you might know, Microsoft has released a service pack for .NET Framework 3.5 few days ago. Let’s see what it adds to the standard framework:

  • ASP.NET Dynamic Data mechanism for quick scaffolding of the database operations
  • ADO.NET Data Service framework, with REST-based service to expose data for its consumption by web services and WCF clients
  • Entity Framework for ADO.NET, an middle-layer ORM and query mechanism that provides an abstract view of the data, detached from its physical implementation
  • Improvements to the CLR engine (mainly for permissions and performance) and other components of the framework

You can download the SP1 from here.

Also, the .NET 3.5 training kit I was referring to before has been updated to the SP1 version. You can grab it here.

Page.Trace and System.Diagnostics.Trace Comparison

More often than not, I see how newbie .NET programmers are confused when trying to trace ASP.NET applications. The origin of the confusion is that .NET Framework exposes two different tracing mechanisms when you are developing web applications.

General .NET Framework Tracing

Firstly, you have the standard low-level System.Diagnostics.Trace class. This class allows us to emit tracing information within our code, using Trace.WriteLine() methods. We also need a TraceListener, which will allow us to capture this information into a storage we choose. .NET Framework does the plumbing for us when we associate a TraceListener with an application, using its configuration file.

There are several pre-made TraceListener implementations already available, such as TextWriterTraceListener (which writes the tracing information into a text file) and EventLogTraceListener (which writes the same information in a specified event log, even if it’s in a remote computer).

ASP.NET Tracing

ASP.NET offers another tracing mechanism for web developers, based on System.Web.TraceContext class. This is an object that lives in ASP.NET page, just like Controls, Request, Response and so on. We have to enable tracing in web.config file, and we can also set it up to display the tracing information on the page itself (setting pageOutput=’true’) or using ASP.NET trace viewer (appending /Trace.axd to the page URL). For example:

<trace pageOutput="true" enabled="true"/>

You can emit tracing information from ASP.NET code using Write or Warn methods of the Page.Trace property. The difference is that Warn messages are displayed in red.

The best thing is that ASP.NET runtime adds a lot of information by default, such as:

  • timing of the main page events, so we can check how long it takes
  • page controls hierarchy tree
  • server variables
  • request and response information

in addition to our own tracing information, as displayed below:

image

Summary

We can use System.Diagnostics namespace objects to trace in ANY .NET Framework application, including ASP.NET ones. But, for ASP.NET applications, we have a powerful tracing mechanism already set in place by Page.Trace property.

More Information

.NET Tracing Walkthrough
System.Diagnostics.Trace Class
ASP.NET Tracing

How to Clear ASP.NET Web Cache

In ASP.NET you can remove an item from the web cache by invoking Cache.Remove(string ID). But, what happens when, for some reason, you want to quickly clear the whole cache? There’s no Clear or RemoveAll method in System.Web.Caching.Cache object.

WORKAROUND

You can iterate over all the entries and remove them by their key (or ID), using the GetEnumerator method of the Cache class.

IDictionaryEnumerator enumerator = Cache.GetEnumerator();

while(enumerator.MoveNext())
{
Cache.Remove(enumerator.Key)
;
}