ASP.NET Web Pages vNext or Razor Pages

4.33 (6 votes)

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.

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.

You might also like...

Date Posted:
Last Updated:
Posted by:
Total Views to date: 3152

15 Comments

- Don

Thanks Mike! Wonder if the MVC Lite is the Controllerless Views approach referred to on the Web Dev and Tools Blog?

https://blogs.msdn.microsoft.com/webdev/2016/06/27/announcing-asp-net-core-1-0/

- Mike

@Don

Yes, it is. I meant to include that term in the article. I will update it a bit later accordingly.

- infocyde

Curious to see if a new release of webmatrix will happen or if that is dead in the water.

- Mike

@Infocyde

There's no doubt about that at all. WebMatrix has been dead in the water for quite some time. The official position is that it is no longer being actively developed. The choices for developing Razor Pages will be Visual Studio and Visual Studio Code.

- Satyabrata

Thanks for sharing

- Curt Smith

I am only slightly disappointed to hear that WebMatrix is officially dead, because I suspected this was unfortunately the case. I thought it was a great little program and wish it had continued. However, Visual Studio Code seems to more than make up the difference for me.

- cyrus

I think adding these features to webpages make it complicated. msft forget webpages goal so we move to php and forget all msft technologies too.

- jared

hi mike, just for clarification, is viewpages something different from webpages? is webpages still on the .net core roadmap and will be simple as it is? thanx

- Mike

@jared

I can' provide any clarification on that. I asked the same question on GitHub about a month ago and no one on the team has replied yet.

It wouldn't surprise me to learn that no final decisions have been made.

- yousaid

Increasingly, learning a Microsoft tool is no longer worth the return on investment. Too many tools introduced and killed once developers become familiar with it. WebMatrix is a good example of this stop and go from MS.
yousaid

- dave

Do SimplemembershipProvider in viewpages is supported?

- Mike

@dave

Nope, and as far as I can tell, there are no plans to introduce an equivalent helper for membership

- cyrus

I've got some news. As Damian stated in this link: https://github.com/aspnet/Mvc/issues/5208

“We already have a product called "ASP.NET Web Pages". This feature is not a new version of this product.“

- Mark

Although "Web Pages" was removed from the roadmap, has it just been renamed to "Razor Pages"?

- Mike

@Mark

Web Pages has not been renamed to Razor Pages. Razor Pages is something different altogether. At the moment, it seems that Web Pages has been abandoned in favour of the new Razor Pages framework.

Recent Comments

Sivu 19/10/2016 08:21
In response to Entity Framework Core TrackGraph For Disconnected Data
Oh that's very very very nice ! Thanks for the write up Mike, much appreciated for the taking the to...

Mark 12/10/2016 16:42
In response to ASP.NET Web Pages vNext or Razor Pages
Although "Web Pages" was removed from the roadmap, has it just been renamed to "Razor Pages"?...

Satyabrata 12/10/2016 09:20
In response to Entity Framework Core TrackGraph For Disconnected Data
Nice article. Please write more articles featuring ASP.Net web pages. Thank you...

Julian 26/09/2016 14:27
In response to Loading ASP.NET Core MVC Views From A Database Or Other Location
Fantastic, many thanks Mike! Had got half way down this road before finding your article - saved...

Abolfazl Roshanzamir 14/09/2016 05:36
In response to Loading ASP.NET Core MVC Views From A Database Or Other Location
Nice article. Thanke you so much ....

cyrus 02/09/2016 15:12
In response to ASP.NET Web Pages vNext or Razor Pages
I've got some news. As Damian stated in this link: https://github.com/aspnet/Mvc/issues/5208 “We...

Simon 01/09/2016 08:00
In response to Loading ASP.NET Core MVC Views From A Database Or Other Location
Thanks Mike, nice post and exactly what I was looking for. Like you said, I think I'll opt to the...

dave 20/08/2016 14:57
In response to ASP.NET Web Pages vNext or Razor Pages
Do SimplemembershipProvider in viewpages is supported?...

Steven 18/08/2016 04:40
In response to Entity Framework Code First and Stored Procedures
Can you provide the directives (using statements) you're using for EF7 example?...

yousaid 17/08/2016 22:08
In response to ASP.NET Web Pages vNext or Razor Pages
Increasingly, learning a Microsoft tool is no longer worth the return on investment. Too many tools...