Tuesday, June 4, 2013

Controller in MVC4 sample and explanation

To create an empty controller, right-click on the Controllers folder and select Add → Controller…,
entering the name HomeController.
The wizard will ask you if you’d like it to generate Create, Update, Delete, and Details actions for you. We won’t use those actions in this book, but feel free to let the wizard generate them for you if you’re interested to see what they look like. The wizard will create a new class file with one action named Index:

using System.Web.Mvc;
namespace MvcRazorBlog.Controllers
{
     public class HomeController : Controller
    {
         public ActionResult Index()
        {
            return View();
        }
    }
}

According to ASP.NET MVC’s default routes, the HomeController’s Index action handles requests for the site’s home page (the root of the site without specifying any file or folder names).
So, the next step is to reproduce the same data access logic that the Default.cshtml page uses to retrieve Post data.

No comments:

Post a Comment