MVC4-Controller (Controller)

Source: Internet
Author: User

Mvc4 controller )! An example of a music store!

Start to write our first contrller!

Our first controller is mainly responsible for the following points:

  • The homepage lists your stores for music genre
  • Click a genre to browse the target webpage. This page lists all music albums.
  • Click an album details page to display a specific music information album.

OK! To create our first controller. For details, see 1.

Figure 1. Name the controller "storecontroller ".

Start to write our first action )!

When the storecontroller controller is created, vs automatically creates (generates) an index method (Action) for us. This is the homepage of the music store behind me, which shows our music genre, we need two more methods to implement the other two cases!

These action methods (parameters) are called controller actions in your controller. As you can see, homecontroller. Index () is used to respond to URL requests, perform corresponding operations, and return the operations to the browser.

Let's take a look at how action works:

View code

//// GET: /Store/public string Index(){return "Hello from Store.Index()";}

A string returned by the index method above, instead of an actionresult type! Let the other two action Methods return a string of characters like the index method. The Code is as follows:

View code

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace MusicStore.Controllers{    public class StoreController : Controller    {        //        // GET: /Store/        public string Index()        {            return "Hello from Store.Index()";        }        //        // GET: /Store/Browse        public string Browse()         {            return "Hello from Store.Browse()";        }        //        // GET: /Store/Details        public string Details()         {            return "Hello from Store.Details()";        }    }}

OK. When we finish writing these things, we can access several urls:

  • /Store
  • /Store/browse
  • /Store/details

We tried to access "/store/details". The running result is as follows: 2.

Figure 2

After observation, we can draw some conclusions:

  1. Browsing/store/details causes the storecontroller class's methods (details) to be executed without any additional configuration. This is a route action.
  2. We use Visual Studio to create a controller class, which is a very simple class. We know that this is a controller class that inherits system. Web. MVC. controller.
  3. We didn't use a model or view. Although models and views are very useful, the true heart of controllers in ASP. net mvc. Each request passes through a controller, and some do not need
    Use Models and views.

Parameters in the Controller

For the parameters in the method, the following is a few simple examples, such as the following code:

        //        // GET: /Store/Browse?genre=?Disco        public string Browse(string genre)        {            string message = HttpUtility.HtmlEncode("Store.Browse,Genre =" + genre);            return message;        }

Run the following parameters in the URL. The running result is as follows: 3.

Figure 3.

Summary:

The Controller is the conductor in the MVC application, closely orchestrating the interaction between user model objects and views. They are responsible for responding to the user's input to the corresponding action, operating on the appropriate model object, and then selecting the corresponding view to display to the user. OK. Let's introduce it to you first. I hope you will give more guidance to your predecessors and friends!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.