Alexander Beletsky's development blog

My profession is engineering

Switching from ASPX to Razor view engine

I’ve been moving my application to MVC3 quite time ago. There was absolutely no issues of migration, everything that worked OK in MVC2 worked fine with MVC3. One of major feature that MVC3 brings with is Razor. Razor is view engine which combines HTML and code in very elegant fashion. Just from first sight to Razor you begin to understand how much ASPX sucks (I don’t blame ASPX since it has been designed for WebForms, actually.. and it just does not fit MVC much).

Of cause, till that time I had a lot of already made markup with ASPX. I have register task to switch all my views to Razor. But for a long time it’s being un-touched, I just been either doing some other things or too lazy for that kind of job. Yes, it is not very exiting to change *.aspx to *.cshtml.. Finally I put all my will on that, thinking better late than never.

And you know, it was not so difficult and boring as I originally thought. Here are some obvious tips:

  • Rename file from *.aspx to *.cshtml
  • If you have master page like (Public.Master) change it to _PublicLayout.cshtml (Razor don’t use term Master, but rather Layout)
  • <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Web.Models.LastArticleFromBlogModel>" %> became @model Web.Models.LastArticleFromBlogModel
  • <asp:ContentPlaceHolder ID="head" runat="server"/> became @RenderSection("Head", required: false) in Layout pages
  • <asp:ContentPlaceHolder ID="maincontent" runat="server"> became @RenderBody() in Layout pages
  • <%@Import Namespace="Web.Helpers.Extensions" %> became @using Web.Helpers.Extensions
  • <%: are changed to @ and %> changed to nothing (I simply used Find&Replace for that)
  • At the top of content page
    @{
     ViewBag.Title = "Admin | Admin Dashboard";
     Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
    }
  • For content pages, everything that is inside is Body (no special sections for that)
  • If you need to override Head section, simply @section Head { }
  • Layout could be nested, in nested Layout page you should mention parent Layout, like above

That’s basically it.. Along the way I’ve changed all HTML to comply HTML5 standard and reformatted (with Ctrl K + Ctrl D) and now markup looks definitely better!

One really strange issue I should mention. As soon as I finished and pushed code to github my friend Jenkins immediately picked up changed and deployed them. But I as went to check it, I got 404 Error just from very beginning. After investigation I found the reason - as I was just renaming files *.aspx to *.cshtml, in *.csproj (project file) all of them are became in “None” item, instead on “Content”.. and site publishing just ignored that items. I have to manually change it back, as soon as I committed, it started to work like charm.

So, there are no any technical issues with moving from ASPX to Razor, just don’t be to lazy and do it today :)