TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

ASP.Net MVC Routing

ASP.Net MVC Routing with asp.net tutorial, asp.net introduction, features, project, example, server controls, labels, textbox, button, hyperlink, radiobutton, calender, checkbox, fileupload, events handling, authentication, webforms model binding, html server control, compare validdator, range validator, validation summary, mvc introduction, mvc project, view, validation, entity framework, authentication etc.

<< Back to ASP

ASP.NET MVC Routing

In MVC, routing is a process of mapping the browser request to the controller action and return response back. Each MVC application has default routing for the default HomeController. We can set custom routing for newly created controller.

The RouteConfig.cs file is used to set routing for the application. Initially it contains the following code.

// RouteConfig.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MvcApplicationDemo
{
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 }
            );
        }
    }
}

According to this setup file, Index action of Home controller will be treated as default. First time, when application runs it produces the following output.

ASP Routing 1

If we look at the address bar, it contains only localhost:52174. There is no controller and action is specified because MVC router maps the controller from the RouteConfig.cs.

If we explicitly enter controller and action names in the address bar, it will redirect to the same action. The localhost:52174/Home/Index will produce the same output to the browser.

ASP Routing 2

Now, let's create a new controller and configure it to the route file. Right click on the Controller folder and select add then controller. It will pop up a window. select an empty controller as we did in screen shot.

ASP Routing 3

This controller has some source code, override this with the following code.

// StudentsController.cs

using System.Web.Mvc;
namespace MvcApplicationDemo.Controllers
{
public class StudentsController : Controller
    {
public ContentResult Index()
        {
return Content("This is default student page");
        }
    }
}

To configure this controller in route file, we have made some changes in RouteConfig.cs file. Code is given below.

// RouteConfig.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MvcApplicationDemo
{
public class RouteConfig
    {
public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                name: "Students",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Students", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

This application produce the same output for localhost:52174 and localhost:52174/Students/Index

Because route module of MVC framework maps the browser URL to the RouteConfig file of the project.

Output:

ASP Routing 4

This is same as above even we have entered controller and action names explicitly.

ASP Routing 5




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf