Adding a Controller in ASP.NET Core 1.0 App

In this chapter you will learn
  1. How to add a controller in ASP.Net Core 1.0 App?
  2. HTTPGET and HTTPPOST method
  3. How to receive and send data to view page

How to add a Controller in ASP.NET Core 1.0 App?

In the previous chapter you learned what are controllers and their importance in MVC. In this chapter you will learn how to add a controller and write logic in it. So before telling you more theory about controllers why shouldn’t make a demo program. It will help you to visualize controller clearly.

Fact about Controllers
  1. Controller should be same named as View Folder. For example, if you have a folder in View Home that keeps several view pages then your controller must be same name as folder. In this condition controller name would be HomeController
  2. Every Controller must add suffix Controller For example, HomeController, UserController, ProfileController etc.
  3. To add a controller, Right Click on Controllers Folder Add New Item. Here, select ASP.NET in left panel under Installed template and select MVC Controller Class in central panel.

Example:

In this following example, I will try to achieve following goals.
  1. Create New Project – HelloMVC
  2. Add a folder in View StevenClark
  3. This folder will keep two view pages. Index.cshtml and YourMessage.cshtml Page.
  4. I will add StevenClarkController for this Folder in Controller section.
  5. There will be a text box and submit button on YourMessage.cshtml page. Button press will display text box messages on the screen.

Here we go!

 
1. Open Visual Studio 2015. Go to File New Project. Select Web in Left Panel and then select NET Core Web Application (.Net Core) in middle section. Give your project name HelloMVC and click OK.

2. A Template window will open. Here, select WebApplication in template section. Select No Authentication in Authorization and click OK.

Adding a View

3. Now, go to solution explorer and add a folder StevenClark under Views. Right click on Views Add New Folder. Give folder name to StevenClark.

4. Now right click on StevenClark folder Add New Item. Select MVC View Page and Give Page Name Index.cshtml and click Add.

5. Add one more page in this folder YourMessage.cshtml.

Adding a Controller

6. Now it's time to add a controller. Right click on Controllers Add New Item. Select MVC Controller Class and give name StevenClarkController.cs and click Add button.


Now What Next?

Congratulations! You have successfully added all the view pages and respective controllers. Now, what next? The next is designing your View Page with controls and colors.

7. Open Index.cshtml from StevenClark folder. Add the following code in this Page.

@{
    ViewBag.Title = "Index Page";
}

<h1>Welcome to Steven Clark Page.</h1>
<br />
<a asp-controller="StevenClark" asp-action="YourMessage">Go to YourMessage Page</a>

In this link there are two properties used.
<a asp-controller="StevenClark" asp-action="YourMessage">Go to YourMessage Page</a>

asp-controller tells to go to StevenClark Controller and asp-action tells to find YourMessage action method in this controller.

And as you Know ViewBag is a dynamic property that is used to pass temporary data between controllers and view. If you are not comfortable with view then you must read this single chapter which explains ViewBag, ViewData and TempData.

Learn ViewBag, ViewData and TempData in ASP.NET Core 1.0
8. Open StevenClarkController.cs and add the following code.

using Microsoft.AspNetCore.Mvc;

// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860

namespace HelloMVC.Controllers
{
    public class StevenClarkController : Controller
    {
        // GET: /<controller>/
        public IActionResult Index()
        {
            return View();
        }
        public IActionResult YourMessage()
        {
            return View();
        }
    }
}

Always remember, every view page needs an action method. This Action Method YourMessage() will be responsible to return view page to the browser.

Run the Program

9. Now Press Ctrl + F5 to see the output in browser. Your project will be launch in your browser. Now add /StevenClark at the end of url and Hit Enter.

Your Steven Clark Index Page will be open. Now click on the link Go to YourMessage page. It will land you on a blank Your Message page. Now we will design this page with controls and colors and achieve our desired output.

10. Open YourMessage.cshtml page and add the following code.
@{
    ViewBag.Title = "Your Message Pages";
}

<h1>Welcome to Your Message Page.</h1>
<br />

<form asp-controller="StevenClark" asp-action="PrintMessage">
  Leave Your Messages :  <input name="Text1" type="text" />
    <input id="Submit1" type="submit" value="submit" />
</form>

<br />
<h2 style="color:red; background-color:burlywood; padding:10px;">
    @ViewBag.Messages
</h2>

11. Save this file and refresh browser to see the output.

12. Now it's time to write PrintMessage() Action method in StevenClarkController so that submit button can work.

13. Open StevenClarkController.cs and put the following code in it.

using Microsoft.AspNetCore.Mvc;

namespace HelloMVC.Controllers
{
    public class StevenClarkController : Controller
    {
        // GET: /<controller>/
        public IActionResult Index()
        {
            return View();
        }
        public IActionResult YourMessage()
        {
            return View();
        }

       [HttpPost]
        public IActionResult PrintMessage()
        {
            string msg = HttpContext.Request.Form["Text1"].ToString();
            ViewBag.Messages = msg.ToString();
            return View("YourMessage");
        }
    }
}

Explanation:

  1. [HttpPost] – It means that the form data is submitted with Post Method. In Post Method the submitted data is not attached with URL. So it is secure but little bit slow. In the [HttpGet] method, data travels through query string.

    Learn More About [HttpPost] and [HttpGet] methods

  2. Request.Form["Text1"].ToString() – Received TextBox Value
  3. Messages = msg.ToString() – Pass this message to ViewBag.Messages property.
  4. return View("YourMessage") – Now return YourMessage.cshtml View Page to browser.

14. Now Press Ctrl + F5 to run the project. Open StevenClark/YourMessage Page. Input String in TextBox and click the submit button see output.

Summary

In this chapter, you learned How to add and work with a controller in ASP.NET Core 1.0. In the next chapter you will learn more controller example like how to add 2 number and display. If you have any difficulties in running this example then you can leave your problem in comments.
 

Share your thought