Home > DeveloperSection > Forums > How to add a Controller in ASP.NET MVC
Anupam Mishra

Total Post:135

Points:949
Posted on    January-30-2016 12:47 AM

 ASP.Net C#  .NET  ASP.Net  ASP.NET MVC 
Ratings:


 1 Reply(s)
 304  View(s)
Rate this:
Hi Everyone,
 Now, i am working on a very popular topic of ASP.NET i.e. ASP.NET MVC framework. So, i want that can anyone  explain how to adding a controller in a my  mvc application.

Thank you.


Anupam Mishra

Total Post:135

Points:949
Posted on    January-30-2016 6:34 AM

MVC stands for model-view-controller.  MVC is a pattern for developing applications that are well architected, testable  and easy to maintain. MVC-based applications contain:

  • Models: Classes that represent the data of the application  and that use validation logic to enforce business rules for that data.
  • Views: Template files that your application uses to dynamically  generate HTML responses.
  • Controllers: Classes that handle incoming browser requests,  retrieve model data, and then specify view templates that return a response  to the browser.

Let's begin by creating a controller class. In Solution  Explorer, right-click the Controllers folder  and then click Add,  then Controller.



In the Add Scaffold dialog box, click MVC 4 Controller - Empty, and then click Add.
and name your controller "HomeController" and click Add.Now we will write the following code:
using System.Web;
using System.Web.Mvc; 
 
namespace MyApp.Controllers 
{ 
    public class HomeController : Controller 
    { 
        // 
        // GET: /Home/ 
 
        public string Index() 
        { 
            return "Hi guest this is index section; 
        } 
 
        // 
        // GET: /Home/Welcome/ 
 
        public string Welcome() 
        { 
            return "This is the Welcome action method..."; 
        } 
    } 
}

We run this project then it produced following results:



Don't want to miss updates? Please click the below button!

Follow MindStick