I’m looking at the Razor engine and I’m wondering how it’s “different” compared to the initial classic ASP implementation where the server-side and front-end code where in the same page.
Why should I care about Razor?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In classic ASP, you used to have business code in your file (“Fetch stuff from the database and act on it”).
In ASP.net MVC – regardless if you use the ASPX or Razor View Engine – you are dealing with View Logic. Things like “I have 20 Employees, display them in a table” or “If this number is negative, display it in red instead of black”.
The business logic is in the controllers and lower. The controller then passes the business data to the view through a view model. The View then only has code that handles displaying it, which is usually trivial but can have a few logic branches of it’s own (“Display Dates in the Users Locale” or “Display male and female employees in separate tables”)
You can make the mistake of putting business logic here. Say, employees hired before 2008 are eligible to a Certificate of Loyalty. So your table has a column “Print Certificate” that is only displayed for these. The simple, but wrong approach is to put an if-statement:
This works, but is wrong because the view now contains business logic. The correct approach is adding a new bool field to the ViewModel. Since it contains an
IList<Employee>in this example, it means creating anotherEmployeeWithCertificateEligibilityclass, or better, having separate lists for eligible and ineligible employees. It’s somewhat common though to have business logic spill into the view, sometimes in form of an HtmlHelper extension method.Edit: You compare it to the “initial classic asp implementation”. That can mean three things: Classic ASP, ASP.net WebForms or ASP.net MVC with the WebForms/ASPX view engine. My example concerns the first two cases. If you already know the whole MVC stuff and just wonder about the differences between the Webforms and Razor View Engine: Conceptually they are the same, Razor is just a lot less verbose and cleaner.