TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

ASP.Net MVC Controller

ASP.Net MVC Controller with asp.net tutorial, asp.net introduction, features, project, example, server controls, labels, textbox, button, hyperlink, radiobutton, calender, checkbox, fileupload, events handling, authentication, webforms model binding, html server control, compare validdator, range validator, validation summary, mvc introduction, mvc project, view, validation, entity framework, authentication etc.

<< Back to ASP

ASP.NET MVC Controller

Controller is a class that handles user requests. It retrieves data from the Model and renders view as response.

The ASP.NET MVC framework maps requested URLs to the classes that are referred to as controllers. Controller processes incoming requests, handle user input and interactions and executes appropriate business logic.

The ControllerBase class is a base class for all controller classes. It provides general MVC handling. A controller mainly performs the following tasks.

  • It locates for the appropriate action method to call and validate.
  • It gets the values to use as the action method's arguments.
  • It handles all errors that might occur during execution of the action.
  • It uses the WebFormViewEngine class for rendering ASP.NET page.

Note: All controller classes must be named by using the "Controller" suffix.


Create a Controller

We can create controller for the application by adding a new item into the controller folder. Just right click on the controller folder and click add -> controller as given below.

ASP Mvc controller 1

Providing controller name, then click Add.

ASP Mvc controller 2

After adding this controller, as per the conventions project will create a folder with the same name as the controller name in the view folder to store the view files belongs to the controller.

This controller contains the default code like below.

// MusicStoreController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplicationDemo.Controllers
{
    public class MusicStoreController : Controller
    {
        // GET: MusicStrore
        public ActionResult Index()
        {
            return View();
        }
    }
}

To access this controller to the browser, we are adding an index file to the MusicStore folder inside the view folder. This index file contains the following code.

// index.cshtml

<div class="jumbotron">
    <h2>Welcome to the music store.</h2>
</div>

Run this file in non-debug mode by pressing Ctrl+F5. This will produce the following output.

ASP Mvc controller 3




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf