ASP.NET MVC framework uses the ASP.NET routing engine

URL Routing

Agenda

  • Overview
  • Mapping URL to Controller Action Method
  • Applying Constraints on Route parameters
  • Resolving Namespace Ambiguity
  • Ignoring Routes

URL Routing Overview

The ASP.NET MVC framework uses the ASP.NET routing engine, which provides flexibility for mapping URLs to controller classes. You can define routing rules that the ASP.NET MVC framework uses in order to evaluate incoming URLs and to select the appropriate controller. You can also have the routing engine automatically parse variables that are defined in the URL, and have the ASP.NET MVC framework pass the values to the controller action methods as parameters.

Global URL Routing Defaults:

Routes are initialized in the Application_Start method of the Global.asax file. The following example shows a typical Global.asax file that class RourteConfig.RegisterRoutes which includes default routing logic.

public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
// . . .
RouteConfig.RegisterRoutes(RouteTable.Routes);
// . . .
}
}
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
name: “Default”,
url: “{controller}/{action}/{id}”,
defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional }
);
}
}

Implementing Routing for Employee Application:

Add following route methods to the Global.asax file

routes.MapRoute(
"Create", // Route name
"Create", // URL with parameters
new { controller = "Employee", action = "Create" } // Parameter defaults
);
routes.MapRoute(
"Edit", // Route name
"Edit/{Id}", // URL with parameters
new { controller = "Employee", action = "Edit" } // Parameter defaults
);
routes.MapRoute(
"Delete", // Route name
"Delete/{id}", // URL with parameters
new { controller = "Employee", action = "Delete" } // Parameter defaults
);

Note:All the entries must be above “Default” route because otherwise it will take precedence and others will not be useful.

Applying Constraints on Route parameters

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{name}/{id}",
defaults: new { controller = "Home", action = "Index", name="test", id = UrlParameter.Optional },
constraints: new { name="[a-z]{5}", id = @"\d{1,8}" }
);

The above route puts a constraint on ID that it should be between 1 and 8 digits only.

Example 2: Overloaded version of MapRoute

routes.MapRoute("Default ",
"{controller}/{action}/{id}",
new RouteValueDictionary { //Default Values
controller = "Home",
action = "Index",
name="test",
id = UrlParameter.Optional },
new RouteValueDictionary { //Contraints
{ "name", "[a-z]{5}" },
{ "id", @"\d{4}" } });

 Resolving Namespace Ambiguity

If we have two HomeController Classes in different namespaces.

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MyDemoApp.Controllers" }
);
//OR
ControllerBuilder.Current.DefaultNamespaces.Add("MyDemoApp.Controllers");

Ignoring Route URL’s

If routes.IgnoreRoute(“Home/Index/123”); is added to RegisterRoutes method

the url: http://localhost:49165/Home/Index/123

will render HTTP Error 404.0 – Not Found

Understanding ASP.NET MVC?

This article is for the beginners who do not have much idea about MVC. As a beginner all you’ll be knowing is that MVC is a pattern but how do we develop applications using this MVC pattern.

Introduction:

So before understanding what MVC is lets understand first what ASP.NET is?

So ASP.NET is a framework which creates web pages and web sites with the help of html, css, and javascript and severs script.

ASP.NET supports 3 development models:

  1. Web Pages
  2. MVC
  3. Web Form

 

MVC (Model View Controller):

MVC is one of the development models of ASP.NET.

  • The Model represents the data of application (database records)
  • The View displays the data
  • The controller handles the flow of data

MVC Model defines application within 3 logic layers.

  • Input logic(Controller handles Input Logic)
  • Business Logic(Model  handles logic of application data)
  • UI Logic(View display the application data)

 

Model:

I.            This part of application manages data of the application or state of data, i.e. basically model object retrieves data from database.

II.            There is no fixed size or shape of model object because data of one application may not be same as other application.

III.            It includes Validation logic, business logic  and data access logic of an application

E.g. Student object (Model) might retrieves information from database, do some operation on it, validate it and then update information back to Student table in database.

 

Controller:

This part of application controls the flow of application data. Basically it reads from a view, control user input and sends data to model.

 

View:

This part of application display the data, basically it creates UI for data from model and display the data

E.g.: Edit View for Student table that displays textboxes, dropdown list and checkbox on the current state of a Student object.

Advantage:

I.            MVC helps in managing complex task by focusing on one aspect at time, a developer can work on model without depending on views or controller.

II.            Separating the view from rest of application logic enables changing of view technology in future without affecting the rest of the application.

E.g. Client might want view in Silverlight or HTML5

 

MVC vs Traditional Web Form:

I.            It is lighter than Web from because it does not use view state or server based forms or server controls.

II.            It provides better support for test-driven development (TDD)

III.            It has integrated with all existing Asp.net features like Routing, Master Page, Security and Authentication.

So I hope this article has given you a brief idea about what exactly ASP.NET MVC is…..