Summary of ASP. NET MVC5 official tutorial (2) Add a controller,. netmvc5

Source: Internet
Author: User

Summary of ASP. NET MVC5 official tutorial (2) Add a controller,. netmvc5
Summary of ASP. NET MVC5 official tutorial (2) Add a controller

In this chapter, we will briefly explain the concept of mvc, the establishment and use of controllers, and the use of routing.

The MVC design pattern is now a very mainstream development pattern, which has the advantages of being easy to test and maintain. It divides traditional web development into three parts: Model-View-controller.

MVC-based applications include:

Models: indicates the data class of the application and uses the verification logic to force the execution of business rules.

Views: The template file that your application uses to dynamically generate HTML responses.

Controllers: the browser that processes incoming requests, retrieves model data, and then specifies to return the response to the view template in the browser.

The following describes the controller. The Controllers are all placed in the Controllers folder. First, create your own controller, right-click the Controllers folder, and choose "add" to create a base shelf ...:

 

Select MVC 5 controller-null, and click Add.

 

Let's name the Controller HelloWorldController. There must be no fewer controllers. This is an agreement.

 

Open it in the Controllers folder and see the following code:

 

Let's modify him like this:

 

Click Run To get the port for running the project:

Then add the name of our controller to the end:

 

ASP. net mvc calls different controller classes (and different operation methods) based on the input URL ). The default URL routing logic used by ASP. net mvc uses this format to determine which code to call:

/[Controller]/[ActionName]/[Parameters]

You can set the route format in the App_Start/RouteConfig. cs file.

 

We can see that the default controller of the route is Home and the default action is Index. Therefore, we didn't add the action name to the content in the default running Index. To run the content in Welcome (), add/Welcome to the address:

 

In addition to the Controller, the action consists of [Parameters], which is the parameter to be passed. Let's modify the Welcome code:

 

Then run our project. This time, our address is written as follows:

Http: // localhost: 15032/HelloWorld/Welcome? Name = Scott & numtimes = 4.

Effect:

 

Parameters named by the ASP. net mvc model binding system in the address bar are automatically mapped to the parameters specified in your method. Now let's modify the welcome code:

 

Run the project at http: // localhost: 15032/HelloWorld/Welcome/3? Name = Rick

 

Effect:

The third part of the URL matches the ID, because the Welcome operation method has a matching URL specification parameter (ID) in the RegisterRoutes method of the route ).

You can also configure the route by yourself to open RouteConfig. cs under the App_start folder.

This is his default route. The default route is generally placed under the custom route, and the following is a custom route:

 

Run the project at: http: // localhost: 15032/HelloWorld/Welcome/Scott/3.

Effect:

 

This chapter is here.

 

 

 

 

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.