ASP.NET Core MVC provides you with a unified web programming model through which you can create web APIs and web UI. The MVC pattern is called the design pattern. It is essentially separate from any other particular framework or language. It is a way of approaching designing a program that you can apply with any number of frameworks or languages. MVC stands for Model-View-Controller. Let’s see what the function of each part is:

Models: These are called business objects. They are the part of the application but independent of the framework.

Views: How we display data to the user via typically a web page, specifically talking about the templates that generate those web pages.

Controllers: They are the “traffic cops” that handle the requests and move data around between the models and the views. The controller receives the request and decides what to do based on the data which is the part of the request.

  • Let’s see how to actually build a .NET core MVC application and work with controllers to handle requests.
  • In Visual Studio, go to New > Project. Select “Web” from the left side under the category of Visual C#. Now select “Asp.Net Core Web Application (.Net Core)”. Give your application a name, I named it “Learn1MVC”, make sure the two boxes at the bottom right are checked, hit “OK”.
  • There you have another option. Select “Web Application MVC”, hit “OK”. It will fetch a lot of files for us that are already built. Close the first window that shows a welcome screen.
  • Now, on the right-hand side, you have a list of files under the Solution Explorer tab. Open “launchSettings.json” under Properties, then check the section “profiles”. The “IIS Express” is the default service that will run your program.
  • Now go back to Visual Studio and Stop the application. Open “HomeController.cs” under the Controllers tab. There you will see a code for the controller that handles the request for Home Page.

Let’s edit the code and create our own controller!

  • Right click on Controllers folder > Add > New item.

  • Now select MVC Controller Class and name your controller that should always end with ‘Controller’. Let’s name it “LearnersController” and hit “Add”. The code appeared is the shortest possible code already developed by VS for us, only we need to “remove unnecessary usings” by right-clicking on the code.

We have only one method within our controller so far which is “Index”. We will see how default behavior works with this code and later we will customize it. This controller returns something of type IActionResult.
• Let’s use the Content method which is the return type of IActionResult and print some string on our application.

    public class LearnersController : Controller
    {
        public IActionResult Index()
        {
            return Content("Building Controllers");
        }
    }

*After building the solution, navigate to the learners page. We get the following:

Here we get our string “Building the controller” on the web page. Also by typing /Learners/Index, we get the same output as the   default value in this controller is within the Index.

* • Let’s now create a new controller method which will be beyond Index. We have to copy the method already mentioned in the code with slight changes as shown below.

    public class ExitMethodController : Controller
    {
        public IActionResult Index()
        {
            return Content("Exit Controller");
        }
    }

Now navigate to the new controller we just created:

It displays our next string within ExitMethod which is “Exit Controller”.

How to add HTML in our text?

We need this code to turn our Index string into HTML text.

        public IActionResult Index(string name)
        {
            return Content("<h1>Building the controller</h1>", "text/html");
        }

It was rendered as an HTML within the browser as an h1 element.

Related Articles

Creating Repository pattern in ASP.net MVC

C# while Loop

Create a Simple Thread Program Using C#

Last modified: January 31, 2019

Comments

Write a Reply or Comment

Your email address will not be published.