MVC is called Model-View-controller (Model-View-Controller ). MVC is a development application.ProgramThis mode has a good framework architecture and is easy to maintain. Applications developed using MVC generally include the following:
- Controller: the Controller class processes requests sent from a client to a web application, obtains data, and returns the data to the client to display the view of the processing results.
- Model: A Model class represents the data of an application. The data usually has a data validation logic to make the data conform to the business logic.
- View: A View class is a template file used in web applications to generate and display the response results of server-side client requests in HTML format.
In this tutorial, we will fully introduce these concepts and show you how to use them to build an application.
First, let's create a controller class. In Solution Explorer, right-click the controllers folder and click Add-> controller, as shown in Figure 2-1.
Figure 2-1 Add a controller
In the displayed "add controller" dialog box, name the controller "helloworldcontroller" and click "add", as shown in Figure 2-2.
Figure 2-2 Name the Controller
Observe that a new file named helloworldcontroller. CS is added to Solution Explorer, and the file is open, as shown in Figure 2-3.
Modify the opened helloworldcontroller. CS file. In the helloworldcontroller class, createCodeIn the two methods shown in Listing 2-1, the Controller returns an HTML string.
Code List 2-1 create a method in the Controller
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 myWelcomeMethod...";
}
}
In the modified helloworldcontroller controller, the first method is named index. Now let's call this method from the browser. Run the application (Press F5 or Ctrl + F5) and add the "helloworld" path (for example, on my computer, the address in the browser is http: // localhost: 4423/helloworld, and the screen is displayed as 2-4. Because the index method directly returns an HTML string, the string is displayed in the browser.
Figure 2-4 result of running the index method in the helloworldcontroller
In ASP. net mvc, you can call different controllers or control seven different methods based on the input address in the browser.ASP. NET MVCThe default ing logic of is in the following format to determine the Controller or method in the controller.
/[Controller]/[actionname]/[parameters]
The first part of the URL address determines which controller class to call./Helloworld"Ing to the helloworldcontroller controller class. The second part decides which method in the Controller to call. Therefore, "/helloworld/Index" will call the index method of the helloworldcontroller controller class. Because the index method is the default method of the controller class (you can specify the default method of the controller class), you can only enter "/helloworld" to call this method.
In the address bar of the browser, enter "http: // localhost: xxxx/helloworld/welcome" to call the welcome method of the helloworldcontroller class, this method returns "this is my welcome method... "text, so the text is displayed in the browser, 2-5.
Figure 2-5 running result of the welcome method in the helloworldcontroller
Next, let's modify the welcome method so that some parameters can be passed to the method in the URL address bar (for example:/helloworld/welcome? Name = Scott & numtimes = 4 ). The modified code is as follows. Note that the optional C # parameter is used here. When the numtimes parameter is not used in the URL address, this parameter is set to 1 by default.
Public String welcome (string name, int numtimes = 1)
{
Return httputility. htmlencode ("hello" + name + ", numtimes is:" + numtimes );
}
Run the application and enter "http: // localhost: xxxx/helloworld/welcome?" in the browser? Name = Scott & numtimes = 4 ". The running result is 2-6. The browser automatically maps the parameters in the URL address bar to the input parameters in the welcome method.
Figure 2-6 using parameters in the welcome Method
till now, we demonstrate the working mechanism of "VC" (view and controller) in MVC. The controller returns an HTML string. Obviously, in most cases, you do not want the Controller to directly return HTML strings, because it is too troublesome to encode them. Therefore, we need to use different view template files to generate HTML page files. In the next section, let's take a look at how to use views in ASP. NET mvc3.