URL Routing Of ASP.NET MVC And ASP.NET Web Forms

Introduction

Key Notes To ASP.NET MVC

ASP.NET MVC is open-source software. The ASP.NET MVC is a web application framework developed by Microsoft, which implements the model–view–controller pattern. Asp.Net MVC has Partial Views for code re-usability. In Asp.Net MVC, Views and logic are kept separately. Asp.Net MVC follows customizable syntax (Razor as default).Asp.Net MVC has html helpers.Asp.Net MVC is lightweight and follows MVC (Model, View, Controller) pattern based development model. Asp.Net MVC does not support view state.

Key Notes To Asp.Net Web Form

Asp.Net Web Form is not Open Source.Asp.Net Web Form follows a traditional event-driven development model.Asp.Net Web Form has server controls.Asp.Net Web Form supports view state for state management at client side.Asp.Net Web Form follows Web Forms Syntax.In Asp.Net Web Form, Web Forms(ASPX) i.e. views are tightly coupled to Code behind(ASPX.CS) i.e. logic. Asp.Net Web Form has User Controls for code re-usability.

Url Path Note Of ASP.NET MVC

Asp.Net MVC has route-based URLs which means URLs are divided into controllers and actions and moreover it is based on controller not on physical files.

Url Path Note Of ASP.NET Web Form

Asp.Net Web Form has file-based URLs means file name exist in the URLs must have its physical existence.

Description

 
Here I built one Asp.net MVC and Asp.net web form application.That will show you what the URL Path Concept Between ASP.NET MVC and ASP.NET Web Forms.
 
Link To Source Code
 
 
Steps to be followed For Asp.Net MVC

Step 1

Create a Mvc Project named "MvcWeb".

Step 2

Create class file in Models Folder named "Student.cs".

Code Ref
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.ComponentModel.DataAnnotations;  
  6.   
  7. namespace MvcWeb.Models  
  8. {  
  9.     public class Student  
  10.     {  
  11.         [Display(Name = "Name")]  
  12.         public string StudentName { getset; }  
  13.     }  

Code Description

Add one variable or entity named "StudentName". To display the result in the page associate with label htmlhelper.
  1. public string StudentName { getset; } 
We can control the display of data in a view using display attributes that are found in System.ComponentModel.DataAnnotations namespace.
  1. [Display(Name = "Name")]  
Step 3

Create a controller class file named "HomeController.cs".

Code Ref
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using MvcWeb.Models;  
  7.   
  8. namespace MvcWeb.Controllers  
  9. {  
  10.     public class HomeController : Controller  
  11.     {  
  12.         //  
  13.         // GET: /Home/  
  14.   
  15.         public ActionResult Satyaweb()  
  16.         {  
  17.             return View();  
  18.         }  
  19.   
  20.     }  

Code Description

Here I created one controller action method named "Satyaweb()". Add the namespace reference on Model class that is,
  1. using MvcWeb.Models;  
Step 4

Create one view named "Satyaweb.cshtml" in Views/Home Folder as the same name in controller action method.

Code Ref
  1. @model MvcWeb.Models.Student  
  2. @{  
  3.     ViewBag.Title = "Satyaprakash";  
  4. }  
  5. <h2 style="background-color: Yellow; color: Blue; text-align: center; font-style: oblique">  
  6.     Satyaprakash's Mvc Path Concept</h2>  
  7. <fieldset>  
  8.     <legend style="font-family: Arial Black; color: blue">Student Name</legend>  
  9.     @Html.LabelFor(m => m.StudentName, "Satyaprakash Samantaray")  
  10. </fieldset>  
  11.   
  12.   
  13. <footer>    
  14.       <p style="background-color: Yellow; font-weight: bold; color:blue; text-align: center; font-style: oblique">© @DateTime.Now.ToLocalTime()</p> @*Add Date Time*@    
  15. </footer> 
Code Description

Here I added namespace in view to access properties of student model class file. 
  1. @model MvcWeb.Models.Student 
LabelFor helper method is a strongly typed extension method. It generates a html label element for the model object property specified using a lambda expression. LabelFor() method Signature: MvcHtmlString LabelFor(<Expression<Func<TModel,TValue>> expression) .
  1. @Html.LabelFor(m => m.StudentName, "Satyaprakash Samantaray"
We have specified the StudentName property of Student model using lambda expression in the LabelFor() method. So, it generates <label> and sets label text to the same as StudentName property name. Here labelfor htmlhelper will show the name "Satyaprakash Samantaray". 
 
Step 5

Set as start page In mvc.

Go to this file path "MvcWeb\App_Start\RouteConfig.cs". Open the file called "RouteConfig.cs".

Code Ref
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Web.Routing;  
  7.   
  8. namespace MvcWeb  
  9. {  
  10.     public class RouteConfig  
  11.     {  
  12.         public static void RegisterRoutes(RouteCollection routes)  
  13.         {  
  14.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  15.   
  16.             routes.MapRoute(  
  17.                 name: "Default",  
  18.                 url: "{controller}/{action}/{id}",  
  19.                 defaults: new { controller = "Home", action = "satyaweb", id = UrlParameter.Optional }  
  20.             );  
  21.         }  
  22.     }  

Code Description

So here I set start page :
  1. defaults: new { controller = "Home", action = "satyaweb", id = UrlParameter.Optional } 
 At page load time the controller Home and action method name satyaweb of home controller will be loaded.

OUTPUT OF MVC URL

http://localhost:1064/home/satyaweb
 
Here home is the controller name and satyaweb is the controller action method name.

 
Steps to be followed For Asp.Net Webform

Step1

Create a Asp.net Webform Project named "AspxWeb".

Step2


Create a new folder named "Home".



Step3

Then add webform named "Satyaweb.aspx".

Code ref of Satyaweb.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Satyaweb.aspx.cs" Inherits="AspxWeb.Home.Satyaweb" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title>Satyaprakash</title>  
  7. </head>  
  8. <body>  
  9.     <form id="form1" runat="server">  
  10.     <div>  
  11.         <h2 style="background-color: Yellow; color: Blue; text-align: center; font-style: oblique">  
  12.             Satyaprakash's Webform Path Concept</h2>  
  13.         <fieldset>  
  14.             <legend style="font-family: Arial Black; color: blue">Student Name</legend>  
  15.             <asp:Label ID="Satyalbl" runat="server" Text="Label"></asp:Label>  
  16.         </fieldset>  
  17.     </div>  
  18.     </form>  
  19. </body>  
  20. <footer>      
  21.         <p style="background-color: Yellow; font-weight: bold; color:blue; text-align: center; font-style: oblique">©<script>document.write(new Date().toDateString()); </script></p>      
  22.     </footer>  
  23. </html> 
Code Description

Here I added label server control named "Satyalbl" .
  1. <asp:Label ID="Satyalbl" runat="server" Text="Label"></asp:Label> 
Code ref of Satyaweb.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. namespace AspxWeb.Home  
  9. {  
  10.     public partial class Satyaweb : System.Web.UI.Page  
  11.     {  
  12.         protected void Page_Load(object sender, EventArgs e)  
  13.         {  
  14.             Satyalbl.Text = "Satyaprakash Samantaray";  
  15.         }  
  16.     }  

Code Description

In the page_load() event I added label control id with associate result which will show in webpage during load time.

Step4

Set start page follow below image .



OUTPUT OF WEBFORM URL
 
http://localhost:1066/Home/Satyaweb.aspx

Here the Home is the name of the folder and Satyaweb.aspx name of the webform inside the Home folder.



Notes

In MVC , URL is mapped to a controller action method. Where as in web forms application, the URL is mapped to a physical file.

Module Summary
  • What is Asp.net MVC.
  • What is Asp.net Webform.
  • The url path concept between ASP.NET MVC and ASP.NET Web Forms.
  • Set start page in ASP.NET MVC and ASP.NET Web Forms.


Similar Articles