Creating a Controller C#


This is next series of MVC For Beginner article. In this series we will learn MVC concept Controller.
You learn how to create controller using visual studio menu option add controller and using simple class.
-Controller-It’s responsible for getting user request, handle input data and saving data and send response to the user.
You need to know Controller-
·         Controller name end with Controller Word. For Example: TestController.
·         Controller is a simple class which inherited from Syestem.web.Mvc.Controller class.
·         The controller class expose controller action.  Action is a method of controller class. Action called by using URL. When you enter the specific URL in Address bar of browser.
              For example if you enter URL: http://localhost:58515/Test/Message
              In this case Test is Controller name and Message is Action (method) which return String
              Message on browser.
->Action (Method) should be public. If you create any private Action- method which cannot access by URL.
->Action (Method) cannot be overloaded.
->Action (Method) cannot be a static method.
                               
 Using Add controller option-
The simple way to create controller is to right click on Controller folder and then select option Add, controller option.

After selecting option Add Scaffold dialog box appear. In this dialog you need to select MVC5 Controller –Empty. If you want to create controller with read/write action then select option as per requirement.
 After selection above option Add controller dialog box appear with default name of controller.
Note- First part of controller name is already highlighted. You should keep in mind every controller name end with word Controller. If you try to give controller name without controller suffix then you cannot access controller.
 Controller\DemoController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCDemoArticle.Controllers
{
    public class DemoController : Controller
    {
        // GET: Demo
        public ActionResult Index()
        {
            return View();
        }
    }
}



Creating Controller class
In ASP.NET MVC controller is a simple class so you can also create controller using in this way. You just need to right click on controller folder and then select menu option Add, Class.

 After selecting menu option Add New Item dialog appear with various template.
->Here you need to select class template. You also need to focus on naming of controller class.
->You need to make sure your controller class name end with word Controller.
->Put controller class name TestController.cs and press Add button.
->After adding class you need to inherit your class with System.Web.Mvc.Controller .

New controller class: Controller\TestController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCDemoArticle.Controllers
{
    public class TestController:System.Web.Mvc.Controller
    {
        public string Message()
        {
            return "Hi This Priti";
        }

    }
}

  You can call this controller Action (Method) using below URL:
You can change port number of URL because it’s different in your local machine.
Author

Priti Kumari

I am technical blogger.I blogs at www.c-Sharpcorner.com and https://aspdotnetmyblog.blogspot.in/.

No comments:

Post a Comment