HOW TO ADD A CONTROLLER IN ASP.NET MVC

Anupam Mishra

Total Post:135

Points:949
Posted by  Anupam Mishra
 628  View(s)
Ratings:
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.
  1. Anupam Mishra

    Post:135

    Points:949
    Re: How to add a Controller in ASP.NET MVC

    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:


Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!