ASP MVC5 Introductory Learning series ②

Source: Internet
Author: User
Tags string back

Original: ASP. MVC5 Introductory Learning Series ②

Add a controller (director)

Because we use the ASP. NET Mvc,mvc is ultimately a set of frameworks, so we still need to follow it to play, or better use to facilitate our development, if the concept of MVC is a little vague, you can go to my previous write the first time ASP. NET MVC 3.0 "Hello World! "In the beginning part of this is elaborated. Assuming that all the learning with me is about the concept of MVC, let's start with the following steps:

Now let's start adding a controller, as follows:

We choose to create an empty controller and then click OK:

Here we need to give the controller a name, I have a stereotype here is called HelloWorld, and then click OK such as:

Here comes an action (action) that returns a view (), which means that here vs will produce a method to return the attempt when we create the controller, which does not have any action to accept the HTTP The GET request HelloWorld over to execute the method. If you are careful, you will find in VS we create helloworldcontroller at the same time, it silently in the views file for us silently created a HelloWorld folder, so that V C in some mechanism is not corresponding, this is a framework convention.

OK, let's simply play with this we created a good controller, the code is as follows:

Here are 2 ways to write, of course, I returned a string, and then we in the browser simulated HTTP to request our Helloworldcontroller inside we write the method of putting back the string, to see if it will correspond to our originating request, we run up our program as follows:

Then we started to simulate HTTP to start asking for our write Helloworldcontroller, specific operations such as:

We ask it to see if we can return the string we need to print to the browser with the following results:

Hellowroldcontroller responds to our request, and returns the string back to print to the browser and we check that the way we initiated the request is not not a get, but using Firefox's developer tools we can clearly see how the request works:

In fact, through the above process we can clearly see the MVC powerful routing mechanism, this I seem to have said several times O (^▽^) O, but MVC is in this place strong yes, so want to learn the MVC words or more to understand the routing mechanism of things, This rookie is also the introduction is not said so advanced, to see the VS in the creation of the application, the framework itself configuration of the route, most of the time this self-configuring the route is enough for our application development. Take a look at the project's RouteConfig.cs configuration file, where he is located in the project location as follows:

In fact, we just simulated the HTTP request Helloworldcontroller the operation of the classic implementation of the above configuration file configuration. When we request HelloWorld, the routing mechanism maps our request to the Helloworldcontroller Class (Controller), according to the configuration file configuration described above, the second URL fragment should be the action (action/method) is the method name, Of course you're going to ask me just now. Index this method, why the result will be the string of the index method is not the return string of the Welcome method. Here I have a personal point of view, the wrong words also hope that friends advice! Because we didn't give it the name (method) of the action in the controller, the routing mechanism here retrieves the default method, and the index method is retrieved here. And that's why it automatically creates a default action method every time we create a controller, and this place is probably one of its functions ... Well, that's a lot to talk about, and then we continue to simulate the HTTP request under our second method, see if we can return the string we want, the result of the request access is as follows:

When we request the welcome () method, we return the string we want to get printed to the page. Also want to explain, the same HTTP impersonation request, two methods are get requests, you think that method execution speed?

Haha of course is the second, here someone will ask why, because the second specifies the action name (method name) all the routing mechanism according to the method you provide the name of the direct go to find, and the first routing mechanism has a retrieval or check the action in the inside may be slightly slower than the second method, Let's take a look at the information returned in the second staff tool is as follows: Does the granularity detail explain the above problem? This is purely a personal point of view.

We can change our welcome () method Slightly, modify the following:

We then request the method in the simulated HTTP-link and parameters to see if we can achieve our intended purpose, and operate as follows:

Such a spell link request is absolutely OK, but so that people can see me this link is to wear parameters past access method, this time most people will say that you are too garbage, too weak! This is not very easy to be a variety of explosive chrysanthemum!o (^▽^) o In fact, I also absolutely write rubbish, things in the address bar is naked waiting to burst chrysanthemum, how to do AH! A friend will stand up and say simple ah I write a cryptographic decryption class in the parameters when the encryption in the location of the decryption, can this practice is available, of course, the novice has also done this kind of thing. But we are now using MVC,MVC can customize the route, so that, for this URL, we can configure a mechanism, and then similar to the URL will follow our mechanism is not better, of course, my previous article also has this aspect of things in the inside, interested words can be turned upside down.

Let's start by configuring such a routing mechanism, RouteConfig.cs inside the configuration Oh, can not casually pull a class to configure, casually configure the words can also, that is, you have to find a way to let the routing mechanism know your configuration on the line, we are in the RouteConfig.cs inside the configuration of our routing mechanism, configured as follows:

Adding such a routing mechanism, we will again simulate HTTP requests under our welcome () method, roughly as follows:

Such a request URL is not much more beautiful ha! However, our request and we want is not the same thing, we give 24 out is 0, here is because we this welcome here the parameters of the problem, we simply changed, changed to the following code:

Then we simulate the HTTP request at a time, and the request results are as follows:

OK, let's share it here for the time being today. O (^▽^) o!

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.