Understanding Model, View, Controller (MVC)

Home > Search
  by

CONTROLLER

In Solution Explorer, expand Controllers, and select HomeController.cs Following is an sample HomeController.cs file for example.com. This controller will produce 3 pages:

  • www.example.com/Home/Index
  • www.example.com/Home/About
  • www.example.com/Home/Contact
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace example.com.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult Contact()
        {
            return View();
        }
    }
}

 


VIEW

In Solution Explorer, expand View > Home, and there will be 3 razor objects:

  • Index.cshtml
  • About.cshtml
  • Contact.cshtml

 

By default, the "About" view will likely contain the following markup:

@{
    ViewBag.Title = "About";
}
<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>

 

With the following markup, the "About" page would look like this:

 

Even if you remove all of the markup from the "About" view, www.example.com/Home/About will still produce the following.The content displayed comes from the _Layout.cshtml and _LoginPartial.cshtml files under /View/Shared.



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments