ASP.Net MVC 5 Using Visual Basic: Adding Controller

Introduction

Today, I am creating the second article of my ASP.NET MVC 5 using Visual Basic Series and in this article we'll learn to add a new controller. In the previous article we learned Getting Started with MVC 5 using Visual Basic.

Overview

Let me provide you a basic introduction to MVC. MVC is an acronym of Model View Controller. It is the design pattern for developing ASP.NET Web Applications and used to do the decoupled architecture. There are mainly three main characteristics used here, given below:

  • Model: It represents the data (entities) defined in the web application and use the validation to implement the business rules for the data.
     
  • View: It generates dynamically the HTML response for the application. These are the template files.
     
  • Controller: It handles the request of the browser, communicate with the model and designates the view that is returned as a response to the browser.

So, let's start to work with this and add a new controller with the following procedure:

  • Adding Controller
  • Working with Controller
  • Running the Controller

Adding Controller

In this section, we'll add the new controller to the Controllers folder in the application. Use the following procedure to do that.

Step 1: Just right-click on the Controllers folder and click on the Controller

Adding Controller in MVC 5

Step 2: Choose the MVC 5 Empty Controller in the Add Scaffold wizard.

Adding MVC 5 Empty Controller

Step 3: Enter the controller name as "SampleController".

Add Controller in MVC 5

Step 4: Edit the new controller code with the following code:

  1. Imports System.Web.Mvc  
  2. Namespace Controllers  
  3.     Public Class SampleController  
  4.         Inherits Controller  
  5.         ' GET: Sample  
  6.         Function Index() As ActionResult  
  7.             Return View()  
  8.         End Function  
  9.         Function SampleCricketer() As String  
  10.             Return "This is Sample Controller of <b>Crickter</b> App"  
  11.         End Function  
  12.     End Class  
  13. End Namespace 

In the code above, a new SampleCricketer() function is created. This function returns a string of HTML. Let's request the new controller method named SampleCricketer() in the browser.

Step 5: Run the application and enter the URL in the browser like: http://localhost:59526/Sample/SampleCricketer. The browser will look at the SampleCricketer method in the Sample Controller and it'll look like this:

Controller in Browser

Working with Controller

As you see in the URL, the browser is invoked by ASP.NET MVC. It can request many controllers that depend upon the URL. The default URL routing format used by MVC is as in the following:

/[Controller]/[ActionName]/[Parameters]

The default routing URL for the MVC application is as follows:

  1. Public Module RouteConfig  
  2.     Public Sub RegisterRoutes(ByVal routes As RouteCollection)  
  3.         routes.IgnoreRoute("{resource}.axd/{*pathInfo}")  
  4.         routes.MapRoute(  
  5.             name:="Default",  
  6.             url:="{controller}/{action}/{id}",  
  7.             defaults:=New With {.controller = "Home",   
  8. .action = "Index", .id = UrlParameter.Optional}  
  9.         )  
  10.     End Sub  
  11. End Module 

You can find it in the App_Start/RouteConfig file. As you can see, the default contrller route is the Home controller and the default action method is index.

The index action is the default method of the controller. If we want to invoke the Index action method in the browser, we do not need to write it in the browser URL.

You can proceed with the following procedure.

Step 1: Change the RouteConfig code as follows:

  1. Public Module RouteConfig  
  2.     Public Sub RegisterRoutes(ByVal routes As RouteCollection)  
  3.         routes.IgnoreRoute("{resource}.axd/{*pathInfo}")  
  4.         routes.MapRoute(  
  5.             name:="Default",  
  6.             url:="{controller}/{action}/{id}",  
  7.             defaults:=New With {.controller = "Sample",   
  8. .action = "Index", .id = UrlParameter.Optional}  
  9.         )  
  10.     End Sub  
  11. End Module 

In the code above, we change the controller to Sample. This would cause the Sample controller to run as a default.

Step 2: Change the Controller code as follows:

  1. Imports System.Web.Mvc  
  2. Namespace Controllers  
  3.     Public Class SampleController  
  4.         Inherits Controller  
  5.         ' GET: Sample  
  6.         Function Index() As String  
  7.             Return "This is the <b>Default</b> action of Sample Controller App"  
  8.         End Function  
  9.         Function SampleCricketer() As String  
  10.             Return "This is Sample Controller of <b>Crickter</b> App"  
  11.         End Function  
  12.     End Class  
  13. End Namespace 

Step 3: Run the application and this time you do not need to enter the URL in the browser to run your controller. You have already defined it as a default URL.

Running Default Controller

Running the Controller

In this section we'll run the controller from the main MVC home page. So just restore the editing in the RouteConfig file. Follow the procedure below.

Step 1: Restore the default code in the RouteConfig file.

Step 2: Open the Views/Shared/_Layout.cshtml file and update the code from the highlighted code below:

  1. <ul class="nav navbar-nav">  
  2.     <li>@Html.ActionLink("Home""Index""Home")</li>  
  3.     <li>@Html.ActionLink("About""About""Home")</li>  
  4.     <li>@Html.ActionLink("Sample""Index""Sample")</li>  
  5.     <li>@Html.ActionLink("Contact""Contact""Home")</li>                      
  6. </ul> 
Step 3: Run the application. From the Home Page open the controller named Sample.

MVC 5 Home Page

Step 4: Now you can view your Index() method that returns the HTML string as shown below:

Running Controller in MVC 5

See, this time the URL of the browser shows the controller name. You do not need to pass the Index in the URL also.

That's it for now.

Summary

This article described how to add and work with the controller in MVC 5 applications. It will also help you to learn the routing of the application. Thanks for reading.


Similar Articles