Learning ASP. NET MVC5 Official Tutorial Summary (ii) adding a controller

Source: Internet
Author: User

Learning ASP. NET MVC5 Official Tutorial Summary (ii) adding a controller

In this chapter we will briefly explain the concept of MVC and the establishment and use of controllers as well as the use of routes.

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

The MVC-based application contains:

Models: A class that represents the data for an application and enforces the data for the business rule using validation logic.

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

Controllers: The browser that processes the incoming request, the class retrieves the model's data, and then specifies the view template that returns the response to the browser.

Below we explain the controller, the controller is placed in the Controllers folder, we first to set up a own controller, right click on the Controllers folder, select Add option in the new scaffolding items ... :

Select MVC 5 Controller - empty , and then click Add.

We give the controller a name, called Helloworldcontroller, the back of the controller must not be less, this is the agreement.

Then open it in the Controllers folder and see the following code:

We made such a change to him:

Then click Run to get the project to run the port:

Then add the name of our controller in the back:

ASP. NET MVC calls different controller classes (and the different methods of operation) based on the incoming URL . the default URL routing logic used by ASP. NET MVC uses such a format to determine which code to invoke:

/[controller]/[actionname]/[parameters]

The format of the route can be set in the App_start/routeconfig.cs file.

You can see that the default controller for the route is home, and the default action is Index, so we just didn't add action The name of his default running content in Index (). We want to run the contents of Welcome () by adding /welcome after the address:

Routing in addition to the controller, theaction is also part of the composition [Parameters] This is the parameter to be passed, we will now modify the Welcome code:

Then run our project, this time our address is written like this:

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

Effect:

The parameters that are named in the ASP. NET MVC Model binding system from the address bar are automatically mapped to the parameters specified in your method. Now let's change the Welcome code:

Then run the project, address Http://localhost:15032/HelloWorld/Welcome/3?name=Rick

Effect:

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

The route can also be configured by itself, opening the routeconfig. CS under the App_start folder .

This is his default route, and the default route is typically placed under its own defined route, with a route defined by itself:

Then run the project, address:HTTP://LOCALHOST:15032/HELLOWORLD/WELCOME/SCOTT/3

Effect:

This chapter is about here.

Learning ASP. NET MVC5 Official Tutorial Summary (ii) adding a controller

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.