DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Anomaly Detection: Leveraging Rule Engines to Minimize False Alarms
  • Impeller, the New Flutter Rendering Engine
  • Building the Next-Generation Data Lakehouse: 10X Performance
  • Using Regular Expressions in Python: A Brief Guide

Trending

  • Three Ways AI Is Reshaping DevSecOps
  • Why Angular and ASP.NET Core Make a Winning Team
  • Automate Your Quarkus Deployment Using Ansible
  • Edge Data Platforms, Real-Time Services, and Modern Data Trends

Switching from ASPX to Razor view engine

Alexander Beletsky user avatar by
Alexander Beletsky
·
Jul. 14, 11 · News
Like (0)
Save
Tweet
Share
10.96K Views

Join the DZone community and get the full member experience.

Join For Free

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 :)

Engine

Published at DZone with permission of Alexander Beletsky, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Anomaly Detection: Leveraging Rule Engines to Minimize False Alarms
  • Impeller, the New Flutter Rendering Engine
  • Building the Next-Generation Data Lakehouse: 10X Performance
  • Using Regular Expressions in Python: A Brief Guide

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: