Asp.net MVC3.0 getting started 2. Controller

Source: Internet
Author: User

Add a controller

MVC stands for model-view-controller. MVC for developing applications,

It is a good architecture and easy-to-maintain mode. MVC-based programs include:

  • Controller: processes external requests, obtains model data, and extracts view templates (returns requests to the client)
  • Model: Business Rules that represent application data and use verification logic to execute data
  • View: Some template files used to dynamically generate HTML responses

This tutorial will include all the above concepts and show you how to use them to create applications.

Let's start to create a control class. In Solution Explorer, right-click the Controller folder and choose add Controller.

 

Name the new controller as "HelloWorldController ". Retain the default templateEmpty controller. Click Add.

 

Note: a new file in Solution Explorer has been created and its name isHelloWorldController. cs.

The file has been opened in IDE.

Create two methods in the public class HelloWorldController of the program block. The Code is as follows.

As an example, the Controller returns an HTML string.

 
using System.Web;
using System.Web.Mvc;
 
namespace MvcMovie.Controllers
{
    public class HelloWorldController : Controller
    {
        //
        // GET: /HelloWorld/
 
        public string Index()
        {
            return "This is my <b>default</b> action...";
        }
 
        //
        // GET: /HelloWorld/Welcome/
 
        public string Welcome()
        {
            return "This is the Welcome action method...";
        }
    }
}

 

Your controller is namedHelloWorldController and the first method is named Index.

Let's call it from a browser. Run the application (Press F5 or Ctrl + F5 ). In the browser address bar

Append "HelloWorld ". (For example, in the following illustration, writeHttp: /localhost: 43246/HelloWorld.)

The following screen is returned on the browser page. In the above method, the Code directly puts back the string.

You told the system to return only some HTML, and it did it!

The first part of the URL determines which controller class to execute. So/HelloWorld ing to HelloWorldControllerClass.

The second part of the URL determines the operation (method) in the Controller class ). So/HelloWorld/IndexTriggered

HelloWorldControllerThe Index method of the class.

Note: The Index method is used as the default method.

This is why the Index method will be called if the method is not explicitly specified.

 

BrowseTtp: // localhost: xxxx/HelloWorld/Welcome. Welcome method return

String "This is the Welcome action method ...".

The default MVC ing is/[Controller]/[ActionName]/[Parameters].

For this URL, the controller isHelloWorld and Welcome are the response methods.

You do not need to use [Parameters].

Let's modify it a bit so that you can pass parameters to the Controller through the URL. (For example,

/HelloWorld/Welcome? Name = Scott & numtimes = 4). Change Welcome

Method, add two parameters as below. Note that the optional parameters of C # are used in the code.

Specify the numTimes parameter. The default value is 1 (when this parameter is not passed)

 
public string Welcome(string name, int numTimes = 1) {
return HttpUtility.HtmlEncode("Hello " + name + ", NumTimes is: " + numTimes);
}
 
Run the application and browse the URL
(http://localhost:xxxx/HelloWorld/Welcome?name=Scott&numtimes=4).
You can try other values for name and numtimes multiple times. The system automatically reflects your method from the URL request string
Parameters.
 
 

In the two examples, the Controller has completed the "VC" in MVC, that is, the view and controller have

Work now. The Controller directly returns HTML. Generally, you do not want the Controller to directly return HTML, because this will become

Very cumbersome code. We usually use a separate view template file to help generate HTML responses.

Let's take a look at how we can achieve this.

 

Next section: View

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.