ASP.Net MVC 5 Using Visual Basic: Adding Controller

This article explains how to add a Controller and how they work in MVC 5 Web Applications.

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:

Imports System.Web.Mvc

 

Namespace Controllers

    Public Class SampleController

        Inherits Controller

 

        ' GET: Sample

        Function Index() As ActionResult

            Return View()

        End Function

 

        Function SampleCricketer() As String

            Return "This is Sample Controller of <b>Crickter</b> App"

        End Function

 

    End Class

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:

Public Module RouteConfig

    Public Sub RegisterRoutes(ByVal routes As RouteCollection)

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}")

 

        routes.MapRoute(

            name:="Default",

            url:="{controller}/{action}/{id}",

            defaults:=New With {.controller = "Home"
.action = 
"Index", .id = UrlParameter.Optional}

        )

    End Sub

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:

Public Module RouteConfig

    Public Sub RegisterRoutes(ByVal routes As RouteCollection)

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}")

 

        routes.MapRoute(

            name:="Default",

            url:="{controller}/{action}/{id}",

            defaults:=New With {.controller = "Sample"
.action = 
"Index", .id = UrlParameter.Optional}

        )

    End Sub

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:

Imports System.Web.Mvc

 

Namespace Controllers

    Public Class SampleController

        Inherits Controller

 

        ' GET: Sample

        Function Index() As String

            Return "This is the <b>Default</b> action of Sample Controller App"

        End Function

 

        Function SampleCricketer() As String

            Return "This is Sample Controller of <b>Crickter</b> App"

        End Function

 

    End Class

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:

<ul class="nav navbar-nav">

    <li>@Html.ActionLink("Home""Index""Home")</li>

    <li>@Html.ActionLink("About""About""Home")</li>

    <li>@Html.ActionLink("Sample""Index""Sample")</li>

    <li>@Html.ActionLink("Contact""Contact""Home")</li>                    

</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.