Razor Websites, lightweight C# web coding






Why use Razor Websites?

The biggest reason to use razor websites, is the speed. Razor websites have almost no routing code, and are much more lightweight than a full framework. They are good for small projects, but for complex data access applications a more robust framework should be used.

Creating a razor website as a web app project

To create a razor website as a web app project, first create an empty web project, and then just add the following nuget packages.


Now you can drop razor files anywhere. Your routes will be the location of your razor pages, so for instance your home page should be Index.cshtml and it should be at the root of your web project. If you had a file called about.cshtml on the root, the route would be /about if it were in a subfolder it would be /subfolder/about.

I even did some tricks where I put razor files in a folder called api and had logic in those views to deserialize the request body to models, and place them in a datastore. This gave the illusion that my ajax calls were somehow hitting some complex API.

Things to note

The @model will not work in razor. You can pass an object to another view during a render, and that file can get the object with this.Model

If you return something other than text remember to set the content type, and don't hesitate to write directly to the output stream.

    this.Response.ContentType = "application/json";

Tagged In:
csharp aspnet