{"id":2899,"date":"2019-04-24T19:11:29","date_gmt":"2019-04-24T13:41:29","guid":{"rendered":"https:\/\/www.idslogic.com\/blog\/?p=2899"},"modified":"2025-02-24T17:56:46","modified_gmt":"2025-02-24T12:26:46","slug":"a-guide-to-asp-net-core-2-razor-pages","status":"publish","type":"post","link":"https:\/\/www.idslogic.com\/blog\/a-guide-to-asp-net-core-2-razor-pages","title":{"rendered":"A Guide to ASP.NET Core 2 Razor Pages"},"content":{"rendered":"<p><span style=\"font-size: 14pt;\">Microsoft has introduced the concept of Razor page with Core 2.0. It is still same as existing MVC in some manner, but with a better approach. In Razor, model and controller are included within Razor page itself.<\/span><\/p>\n<p><span style=\"font-size: 14pt;\">In MVC you need to define viewdata, controller action, routecollection, HTML helper etc. to tie up Model, View and Controller approach. <\/span><\/p>\n<p><span style=\"font-size: 14pt;\">Any change in Model may also disturb the view behavior and it also need fixing at the same time. It makes the solution a little bit costly. Razor page approach is better in this situation where we have Razor view and the entire code behind file. It is less complex than MVC.<\/span><\/p>\n<p><span style=\"color: #003366; font-size: 18pt;\"><strong>Sample Code of Razor Page Using inline Code Within a @functions Block:<\/strong><\/span><\/p>\n<pre><textarea class=\"xml\" name=\"code\">@page\n@model TimerModel\n@using Microsoft.AspNetCore.Mvc.RazorPages\n\n@functions {\n    public class TimerModel: PageModel\n    {\n        public string Message { get; private set; } = \"Time : \";\n\n        public void OnGet()\n        {\n            Message += $\" Time  { DateTime.Now.ToString() }\";\n        }\n    }\n}\n\n&lt;h2&gt;Timer&lt;\/h2&gt;\n&lt;p&gt;\n    @Model.Message\n&lt;\/p&gt;\n<\/textarea><\/pre>\n<p><span style=\"color: #003366;\"><strong><span style=\"font-size: 18pt;\">Sample Code of Razor Page using code behind.<\/span><\/strong><\/span><\/p>\n<p><span style=\"color: #008000;\"><strong><span style=\"font-size: 14pt;\">Razor view<\/span><\/strong><\/span><\/p>\n<pre><textarea class=\"xml\" name=\"code\">@page \"{id:int}\"\n@model TimerModel\n&lt;h2&gt;Timer&lt;\/h2&gt;\n&lt;p&gt;  \n@Model.Message \n&lt;\/p&gt;\n<\/textarea><\/pre>\n<p><strong><span style=\"font-size: 14pt; color: #008000;\">Code behind:<\/span><\/strong><\/p>\n<pre><textarea class=\"xml\" name=\"code\">   public class TimerModel: PageModel\n    {\n        public string Message { get; private set; } = \"Time : \";\n\n        public void OnGet()\n        {\n            Message += $\" Time  { DateTime.Now.ToString() }\";\n        }\n    }\n<\/textarea><\/pre>\n<p><span style=\"font-size: 14pt;\">It is like a webform with all the pros of MVC. It is very useful in page focused operations like reports and CRUD operations. It provides a simple and faster approach to develop a website. It also includes anti-forgery token validation automatically.<\/span><\/p>\n<p><span style=\"font-size: 14pt;\">So, enjoy the website development with Razor pages in ASP.NET Core 2.<\/span><\/p>\n<blockquote>\n<p style=\"box-shadow: 0 0 16px #cccccc; padding: 10px;\"><span style=\"font-size: 18.6667px;\"><span style=\"color: #800000;\"><b>Also Read:\u00a0<\/b><\/span><a href=\"\/blog\/important-changes-in-asp-net-core-3-0\"><span style=\"color: #ff0000;\">Important changes in ASP.NET Core 3.0<\/span><\/a><\/span><\/p>\n<\/blockquote>\n<p><span style=\"color: #800000;\"><strong><span style=\"font-size: 14pt;\">Contributed By:<\/span><\/strong><\/span><\/p>\n<p><span style=\"font-size: 14pt;\">Arunodaya Kumar<\/span><\/p>\n<hr \/>\n<div class=\"newsletter_form\">\n<h2>Sign up our Newsletter!<\/h2>\n<div class=\"emaillist\" id=\"es_form_f0-n1\"><form action=\"\/blog\/wp-json\/wp\/v2\/posts\/2899#es_form_f0-n1\" method=\"post\" class=\"es_subscription_form es_shortcode_form  es_ajax_subscription_form\" id=\"es_subscription_form_6a3a52aebb0a8\" data-source=\"ig-es\" data-form-id=\"0\"><div class=\"es-field-wrap\"><label>Name<br \/><input type=\"text\" name=\"esfpx_name\" class=\"ig_es_form_field_name\" placeholder=\"\" value=\"\" \/><\/label><\/div><div class=\"es-field-wrap\"><label>Email*<br \/><input class=\"es_required_field es_txt_email ig_es_form_field_email\" type=\"email\" name=\"esfpx_email\" value=\"\" placeholder=\"\" required=\"required\" \/><\/label><\/div><input type=\"hidden\" name=\"esfpx_form_id\" value=\"0\" \/><input type=\"hidden\" name=\"es\" value=\"subscribe\" \/>\n\t\t\t<input type=\"hidden\" name=\"esfpx_es_form_identifier\" value=\"f0-n1\" \/>\n\t\t\t<input type=\"hidden\" name=\"esfpx_es_email_page\" value=\"2899\" \/>\n\t\t\t<input type=\"hidden\" name=\"esfpx_es_email_page_url\" value=\"https:\/\/www.idslogic.com\/blog\/a-guide-to-asp-net-core-2-razor-pages\" \/>\n\t\t\t<input type=\"hidden\" name=\"esfpx_status\" value=\"Unconfirmed\" \/>\n\t\t\t<input type=\"hidden\" name=\"esfpx_es-subscribe\" id=\"es-subscribe-6a3a52aebb0a8\" value=\"e12a72702e\" \/>\n\t\t\t<label style=\"position:absolute;top:-99999px;left:-99999px;z-index:-99;\" aria-hidden=\"true\"><span hidden>Please leave this field empty.<\/span><input type=\"email\" name=\"esfpx_es_hp_email\" class=\"es_required_field\" tabindex=\"-1\" autocomplete=\"-1\" value=\"\" \/><\/label><input type=\"submit\" name=\"submit\" class=\"es_subscription_form_submit es_submit_button es_textbox_button\" id=\"es_subscription_form_submit_6a3a52aebb0a8\" value=\"Subscribe\" \/><span class=\"es_spinner_image\" id=\"spinner-image\"><img decoding=\"async\" src=\"https:\/\/www.idslogic.com\/blog\/wp-content\/plugins\/email-subscribers\/lite\/public\/images\/spinner.gif\" alt=\"Loading\" \/><\/span><\/form><span class=\"es_subscription_message \" id=\"es_subscription_message_6a3a52aebb0a8\" role=\"alert\" aria-live=\"assertive\"><\/span><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Microsoft has introduced the concept of Razor page with Core 2.0. It is still same as existing MVC in some manner, but with a better approach. In Razor, model and controller are included within Razor page itself. In MVC you&#8230;<\/p>\n","protected":false},"author":1,"featured_media":2900,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[104],"tags":[295,296],"class_list":["post-2899","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","tag-asp-development","tag-asp-net-development"],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/posts\/2899","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/comments?post=2899"}],"version-history":[{"count":5,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/posts\/2899\/revisions"}],"predecessor-version":[{"id":10651,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/posts\/2899\/revisions\/10651"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/media\/2900"}],"wp:attachment":[{"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/media?parent=2899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/categories?post=2899"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/tags?post=2899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}