So I know I do a lot of blogging about C#, or JavaScript, but I actually do a lot of nodejs apps as well as other languages. For a very long time I have not found the stack of my dreams. .NET has always been very close but there were multiple things about the app model that I was not a fan of. I think NancyFX has been the closest framework to my dreams in .NET land.
Razor Websites, lightweight C# web coding
I was exploring around github, and I stumbled upon an interesting project called Miniblog which was a lightweight blog engine written in c#. The thing that immediately stood out to me was the lack of a .csproj
file.
As I dug around the code I realized this was not a Web App, which most of us were familiar with, but a websites project. I then suddenly realized that the whole thing only used razor!
I am a huge fan of Nancyfx because its much more lightweight than the MVC framework created at Microsoft. To say the least I am a massive fan of small tools, and micro frameworks. So when I realized this whole thing was powered by razor only I was immediately impressed.
I decided to dig around on the internet to see if anyone else was talking about this. I found out quickly that it has been possible for some time, but I didn't find many references about it.
The one thing that bummed me out about the Miniblog example was that it was not a web app. You can use nuget packages will websites, but you cannot make references to other projects in the solution. This was a problem for me, and unlike websites, web app's are precompiled which reduces application startup time.
Hosting NancyFx with OWIN on IIS
So I was quite confused about hosting Nancyfx on OWIN under IIS. Parts of the Nancy wiki led me slightly astray.
Here is the simple guide.
Make sure you Install the following nuget packages (if you havn't already).
Mono, not just a sickness
In the old days, when programming in .NET you were signing yourself up to a lifetime of windows server, however things have changed.
Binding SSL Certs on Windows Installer XML (WiX) deployed Web Applications
This tutorial is about using SSL certs with WiX for IIS websites. For those of you whom didn't know, WiX is an MSI generator. You can even deploy IIS applications with WiX's MSI's.
Anti-Forgery Tokens in NancyFX with Razor
Getting started with anti-forgery tokens in NancyFX with razor views is pretty simple.
To start you need to enable csrf in application startup.
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
Csrf.Enable(pipelines);
base.ApplicationStartup(container, pipelines);
}
Now you need to create a token on the get request that returns the form
Get["/"] = x =>
{
this.CreateNewCsrfToken();
return View["Index"];
};
<!-- more -->
Now in your view you need to render the token
<form method="POST">
Username <input type="text" name="Username" />
<br />
Password <input name="Password" type="password" />
<br />
<input type="submit" value="Login" />
@Html.AntiForgeryToken()
</form>
Finally you need to authenticate the token on the post request
Post["/"] = x =>
{
try
{
this.ValidateCsrfToken();
}
catch (CsrfValidationException)
{
return Response.AsText("Csrf Token not valid.").WithStatusCode(403);
}
//do something
};
Custom error pages in Nancyfx (C# Web Framework)
To do custom error pages in Nancy you must implement an IStatusCodeHandler. This class must provide 2 methods. HandlesStatusCode is a bool that basically should tell Nancy if this class will handle the status code. If this returns true then this class will be responsible for handling the request.
Getting SquishIt to work with Nancyfx and Razor (...and other static content issues)
SquishIt is a content bundler and minification tool. The github documentation contains exaples how to install and use it, and a sample application is provided. However I had some issues getting it to work with razor so I figured I would share these pain points with you.