Last week I mentioned that with .Net Core 2.0 we got something that makes my jaw drop. This “little” feature that made me feel slightly uncomfortable is Razor Pages. In this post, I will show you what it really is or, in fact, what it is not 😊 and if we should be afraid of it.
Razor Pages – what the heck is it?
At first glance, you may think that Razor Pages are Microsoft’s attempt of coming back to the ASP.Net WebForms. Which I, unfortunately, still deal with damn too often so believe me, that would be an epic disaster! And I would stop loving .Net Core instantly! 😛
Ok, but you may ask (you do? 😉 ), why these two technologies seem so similar to me?
Because both of them exist without a need of a controller and all the backend logic is written in PageName.[extension].cs file! Sounds similar, huh?
Yep, Razor Pages are files with frontend in PageName.cshtml file and backend in PageName.cshtml.cs. No Controller needed. But luckily, in *.cshtml.cs file you will not find well known and old-fashioned Page_Load() or Page_Init() methods.
So, what will we find inside a .cshtml.cs? A view model! Additionally, you can add there methods to handle POST and GET requests – their names may be OnGet() and OnPost() (or OnMyPrettyShinyPinkButtonPost() – more about it in the next post). Still looks like WebForms? Not at all!
Let’s look at the project!
Razor Pages are available in Visual Studio 2017 15.3, which I wrote about here. So, we create a new project by choosing Visual C# -> .Net Core -> ASP.Net Core Web Application and select ASP.Net Core 2.0.
From the available templates, we can select Web Application or Web Application (Model-View-Controller). In this tiny demo, I chose Web Application because Razor Pages are there already implemented.
When we expand it, a few standard pages will appear – About.cshtml, Contact.cshtml and so on. BUT, you probably noticed also _Layout.cshtml, _ViewStart.cshtml and 2 more, well known from ASP.Net MVC.
So, what is going on here? The answer is easy – Razor Pages are based on the ASP.Net MVC, so you can benefit from both features. You still can add a controller to your app and handle some pages with the standard MVC way and other with Razor Pages. Yes, you can. But is it a good idea? Well, I don’t know!
In my opinion, things can get easily messy but I will wait with the final word… At least till the moment when I try this solution in a real life situation 😉. As the Microsoft’s defender, I will only mention that the company accents here the fact that Razor Pages are intended for helping developers focusing on the UI rather than coding all the MVC necessary things. Well, I think I still feel like a good mockup is better than a messy project but considering the fact that my code is often messy too, well, I’m not the best person to judge 😉. And what is your feeling here? Write in the comment!
PS In the next post I will write how to code Razor Pages and I will probably send some dummy project to github, so if this topic seems interesting to you, stay tuned – the work is in progress and I hope to publish the article in about 2-3 days!
Featured photo by Jonathan Pielmayer