Using ETag to cache responses in NancyFX

Caching data is usually a good idea, especially when creating APIs for mobile clients and the user may pay for each transferred byte. There are many approaches to caching data (I recommend reading this article), in my last NancyFX project I used ETag.


ETag is a HTTP header that acts as a hash of the data. When the server returns a response, it computes a hash of the data and sends it to the client. When the client requests the data again, it includes the ETag in its request. The server compares the ETag with the hash of the current data and if they match (the data did not change), it returns an empty responses with a HTTP 304 status code.

[Read More]
asp  azure  nancyfx 

Detecting tablets and smartphones in ASP.NET

I recently worked on an ASP.NET application that needed to detect if users were coming from tablets or smartphones. The project used data from to do this detection, but the result were not really good. We needed a better solution, so I came up with using WURFL.

WURFL, the Wireless Universal Resource FiLe, is a Device Description Repository (DDR), i.e. a software component that maps HTTP Request headers to the profile of the HTTP client (Desktop, Mobile Device, Tablet, etc.) that issued the request. Adding WURFL to your ASP.NET application is easy thanks to the WURFL_Official_API Nuget package. The Nuget package also contains definition file, so you just need to update the Nuget package once in a while to get your definition file up to date.

[Read More]
asp  csharp 

C# scripting console for ASP.NET MVC application

In a recent larger ASP.NET MVC project running in Windows Azure I needed to provide the advanced users a way to execute custom scripts directly through the webbrowser. A kind of a scripting console where users can create and execute their own scripts, that interact with the project and automate some tasks (that would otherwise require too many clicks).

Choosing the language

I thought about using Lua but I needed an easy way to integrate with some classes (repositories) used in the project. I decided to use C# as the scripting language and implement the scripting console using Roslyn. Roslyn is a really neat project and I recommend taking a look at it, if you do not already know it.

I extracted the main idea of my implementation of the scripting console and posted it on GitHub. I will walk you through it in this blog post.

[Read More]
azure  csharp  mvc  roslyn  asp 

Detecting encoding of uploaded file in ASP.NET MVC

Uploading a file in ASP.NET MVC is very easy, but there is no easy way to detect the encoding of a uploaded text file. However you can use the fact if you try to read the file with a wrong encoding, you get an DecoderFallbackException. So how do you put everything together?

First, get a stream of the uploaded file.

public ActionResult FromCSV(HttpPostedFileBase file)
    if (file != null && file.ContentLength > 0)
        var stream = file.InputStream;

[Read More]
web  asp  csharp