ASP.NET Web Pages vNext or Razor Pages

Now that the RTM of ASP.NET Core has shipped, the ASP.NET team are looking at the features that were left behind including SignalR and Web Pages. The ASP.NET Core road map always had those items scheduled for inclusion in the first major release after RTM, which will be ASP.NET Core 1.1. Recently, the ASP.NET Team provided an update on GitHub on the goals driving the next version of Web Pages which provides an interesting insight into the potential features of the framework.

UPDATE: It seems I may have jumped the gun. The ASP.NET team are very keen to point out that the feature I refer to below is NOT a new implementation of ASP.NET Web Pages. Web Pages still remains on the roadmap, but how this feature relates to that roadmap entry is currently unclear. I shall provide further updates as things are clarified. However, at the moment it is looking increasingly unlikely that a straight replacement for Web Pages is being planned at all.

Update 2:Web Pages has now been removed from the current Roadmap so it looks like Web Pages is no longer going to be included in ASP.NET Core.

Update 3: Razor Pages has been released in Preview form. It is not Web Pages, but if you liked Web Pages, Razor Pages is well worth looking at.

The first thing to note is that the version of Web Pages that will be supported in ASP.NET Core has yet to be officially named. The feature is referred to as "Razor Pages" throughout the GitHub issue (now changed to MVC Pages), but the issue itself is currently titled "MVC Pages" (now MVC View Pages). In the .NET Core roadmap, it is referred to as (yuk!) View Pages. The term "Web Pages" is so generic that it makes searching for relevant information difficult. Calling it something different will help with this and makes a pretty clean break from the existing Web Pages framework, with which it shares little more than a passing resemblance. The templating syntax will still be Razor, and the files will still be cshtml files, but there will also be significant differences. This is a totally new framework, not a version increment of the existing Web Pages framework. My preference in terms of naming is Razor Pages, as that is likely to create the least confusion.

Built on ASP.NET Core, Razor Pages will sit on top of .NET Core MVC. There are two benefits to this: first, you will be able to build Razor Pages sites that can be deployed on Unix as well as Windows servers, taking advantage of the fact that .NET Core is designed to enable cross-platform development. This provides users with much greater freedom in the hosting packages that they can opt for. The second benefit is that many of MVC's features can be surfaced in Razor Pages.

MVC Features

So what are the key MVC features that the ASP.NET team are considering exposing via Razor Pages? Here's a quick run-down of some of them and a look at how they can improve the development experience.

Model Binding

Model Binding is already available in both MVC and Web Forms. Anyone who has done a fair amount of Web Pages development involving forms will be more than familiar with the amount of code you have to write to assign values from the various Request collections (Forms, QueryString etc.) to variables to pass as parameter values for database operations or other processing. A large CRUD site can involve a huge amount of this type of code. It's boring and repetitive. The model binding system does all of this for you.

The current proposal is to add a feature called Page Actions that works in a similar way to controller actions in MVC. These are methods that take parameters which are hydrated by the model binding feature. It will work something like this:

@class {
    public IActionResult OnPost(Product product)
    { 
        // do something with product
        return View();
    }
}

This method will execute if the page is requested using the Http POST verb, which is the equivalent to checking the condition if(IsPost) at the moment. The product variable will be auto-magically populated with values from Request.Form. The return View() statement will result in the page content being rendered to the browser.

ActionResults

An ActionResult is the return type of a Page Action. MVC features a wide variety of different ActionResult types, including a FileResult for returning files to the browser, JsonResult, ContentResult (for returning strings), RedirectResult (which replaces Response.Redirect) and so on. They make life easier in that they set the right ContentType for the browser (except for the FileResult, which you have to specify).

TagHelpers

TagHelpers are reusable components for generating HTML. They use an HTML-like syntax. There are already a number of built-in TagHelpers for a variety of form fields as well as for generating links, managing caching and so on. In addition, it is easy to create your own.

Strongly Typed Html Helpers

In the current version of Web Pages, you can use Html Helpers to render form fields:

@Html.TextBox("FirstName")

The MVC framework includes strongly-typed versions of these that know about the "model" for the current View. It will be possible to declare the model type for a Razor Page via an @model directive, and then make use of the strongly typed helpers e.g.:

@model Person
...
@Html.TextBoxFor(model => model.FirstName)

The key benefit of this approach is that it provides type safety, with compile-time checking of the helper. In the first example, the code relies on a string. It will fail silently if you attempt to reference the content of the textbox using a different value to the one specified in the helper e.g. Request["ForeName"]. In the strongly-typed example, if you try to use a property name that doesn't exist in the Person class, the code won't compile. Not only that, but strongly typed helpers enable better Intellisense support. When you combine that with Model Binding, you save quite a bit of time and reduce the potential for bugs.

MVC-Light or Razor Web Forms?

One of the more interesting discussion points posted by the ASP.NET team concerns the idea of adding a "code-behind" feature. On further questioning it seems that this is being considered to facilitate code separation and unit testing. These are two of the key drivers behind the introduction of the MVC framework some 8 years ago, so now it appears that the ASP.NET team are contemplating adding an "MVC-Light" option, which removes the ceremony around having controllers which inherit from the Controller class, and having View files in specific locations within a Views folder. The controller will actually be the code-behind, with a one-to-one relationship between controller and view, which is very much like the Web Forms development model based on the Page Controller pattern. Leaving aside the absence of the event driven aspect of Web Forms, the proposed approach will potentially feel quite familiar to Web Forms developers who have yet to embrace MVC - especially when one includes the use of TagHelpers which bare a passing resemblance to server controls. I dare say it will also provoke quite a bit of debate in both directions.

I was originally a little confused about the purpose of this suggestion, but now that Ryan Nowak and Damian Edwards have provided a bit more explanation, I'm quite intrigued to see how it will work out - if it ever passes the "idea" phase.

Data Access

WebMatrix.Data was introduced as the main means for accessing databases in the Web Pages framework. Nothing has been mentioned officially in respect of data access strategies for a Razor Pages framework, but I would like to see WebMatrix.Data ported over to .NET Core to enable its use with Razor Pages. This wouldn't preclude the use of other data access methods such as Entity Framework of course. If WebMatrix.Data is reused, it would be nice to have async support and some methods that accept generic type arguments to return strongly typed data, as well as dynamic return types e.g.:

var data = db.QueryAsync<List<Product>>("select * from Products");

or

var product = db.QuerySingleAsync<Product>("select * from product where id = @0", id);

Summary

There is still a way to go before the next version of Web Pages is introduced, and no doubt things will be added and removed from the list of ideas currently presented. If you want to keep up with development, follow the issue at GitHub.