In this tutorial you will learn Controller part of the ASP.NET MVC application. Basically, MVC has three parts Models, Views, and Controllers. Now you are going to learn the basics of asp.net mvc controller, ignoring models and views for a bit. After this tutorial you will be able to understand how MVC controller works and how to add action or methods in a controller class with single parameter or multiple parameters.
Controllers are the logic part of your ASP.NET MVC application. When you request an MVC application using browser, then a controller is responsible for returning a response to that request. Basically, mvc controller decides which model will be selected, after selecting it takes the data from the model, and then it passes the data to the view.
MVC Controllers control the overall flow of ASP.NET MVC application. MVC Controllers are the C# classes. These classes inheriting from System.Web.Mvc.Controller, which is the built-in controller base class. Controller may have multiple actions (each method in a controller is known as Action). You can invoke these actions through request using browser. The residence of MVC controller in Controllers folder of MVC Application (Solution explorer => Controllers).
ASP.NET MVC Controller
Let’s see how ASP.NET MVC Controller works?
Step # 1: Open Visual studio 2015
Step # 2: Click on file, new and then Project (File => New => Project). It will open a new project dialog.
Step # 3: After showing new project dialog, select Templates from the left pane, choose Web under Visual C#. (Templates => Visual C# => Web). Then in the middle pane, choose ASP.NET Web Application. Then enter the project name ControllerDemo, in the name field, and then change the location of your project if you want in the location field, and click ‘ok’ button. It will show a new dialog.
Step # 4: Choose empty option, and then check the MVC check box under the ‘Add folders and core references for’ and then click ‘OK’ button. It will create a simple ASP.NET MVC Application.
You will see a number of folders in the solution explorer. If you don’t know the purpose of these folders then see the previous tutorial.
Step # 5: Now, run your application. You will see a 404 error, just because your application does not contain anything to run in browser.
Let’s add a simple controller……
Step # 6: Right click on the Controllers folder in the solution explorer, click add, then choose controller (Add => Controller). It will show ‘Add Scaffold’ Dialog.
Step # 7: Choose MVC 5 Controller – Empty, and then click on ‘Add’ button. It will display a controller name dialog.
Step # 8: Enter the name with controller suffix (E.g. StudentController), and then click on ‘Add’ button. Then you will see a new C# file StudentController.cs under the Controllers folder.
Step # 9: Now let’s make a simple controller Actions. First of all, replace the contents of the StudentController.cs file with this following code.
p style=”text-align: justify;”>
The controller methods will return a string. The controller is named StudentController and there is two Actions in this controller Index() and Welcome(). Let’s run your application(Press ctrl + F5 or F5). In the browser, enter this URL http://localhost:60747/Student. And you will see a page look like the following screenshot.
ASP.NET MVC invokes a different Controller classes and Actions within the Controller classes depending on the incoming URL. ASP.NET MVC follows the following URL format to determine which controller will be called.
The first segment of the URL will determine the MVC controller class to execute(“http://localhost:60747/Student”). The second segment of the URL will determine the Action method to execute(“http://localhost:60747/Student/Index ”). And the third segment of the URL will determine the parameters (“http://localhost:60747/Student/Index/Id”), it’s optional. Notice that we had to browse only http://localhost:60747/Student, and the index method was used by default. This is because if we don’t specify action name in URL then Index action will be default.
Same as above, now enter this URL “http://localhost:60747/Student/Welcome” in the browser. Then you will see, a Welcome action method will run and return the string “This is the <b>Welcome</b> Action Method”. Let’s see, in this URL Student is controller, Welcome is action name and parameters part is not used because it’s optional.
Add a controller action with one parameters
Let’s make another controller action, add the following code in StudentController.cs class.
This StudentID action method contains one parameter ID, and the default ID is 1. If you will not pass the parameter in the URL, then it will show the default parameter 1, otherwise you can try different types of IDs.
HttpUtility: It is used for encoding and decoding URLs when processing web requests.
HtmlEncode: It is used to convert a string into an HTML encoding string.
Let’s run your application with this URL “http://localhost:60747/Student/StudentId”.
Now, try this URL http://localhost:60747/Student/StudentId/3.
Add a controller action with Two parameters
Let’s run your application with this URL “http://localhost:60747/Student/StudentRecord?name=Shown&Id=5”