Add a Controller)
Because we use Asp. net MVC, MVC is still a framework, so we still need to follow it to play, or make better use of it to facilitate our development. If the MVC concept is still vague, you can go to Asp. net MVC 3.0 [Hello World!] In the beginning of this section. Assuming that all the things I learned with me understand the concept of MVC, we will start the following steps:
Now, add a Controller as follows:
Create an empty Controller and click "OK:
Here, you need to give the Controller a name. Here I am the old one called HelloWorld, and then click OK, as shown in:
Here comes an action to return the View (), which means VS will produce a method to return the attempt when creating the Controller for us, this method is executed by accepting the http get request HelloWorld. If you are careful, you will find that in VS, when we create HelloWorldController, it silently creates a HelloWorld folder for us under the Views file, in this way, does v c correspond to a certain mechanism? This is also a framework convention.
OK. Let's take a look at the newly created Controller. The Code is as follows:
Here, I casually wrote two methods. Of course, I returned a string. Then we simulate HTTP in the browser to request the method of returning the string in HelloWorldController, check whether it will respond to our request. The program we run is as follows:
Then, we started to simulate HTTP and request our written HelloWorldController. The specific operations are as follows:
We request it to see whether the string we need can be returned and printed to the browser. The result is as follows:
HelloWroldController responded to our request and printed the string back to the browser. Then, we can check whether the request method is GET, using Firefox's developer tools, we can clearly see the request method:
In fact, through the above process, we can clearly see the powerful Routing Mechanism of MVC, Which I seem to have said many times o (^) o, but MVC is strong in this place, so if you want to learn MVC well, you still need to learn more about its routing mechanism. This cainiao is not so advanced as it is getting started, let's take a look at the routes configured by the framework itself when VS creates an application for us. In most cases, this route configuration is enough for our application development. Let's take a look at the RouteConfig. cs configuration file of the Project. Its location is as follows:
In fact, we have just simulated the HTTP request HelloWorldController and executed the configuration file above. When we request HelloWorld, the routing mechanism maps our requests to the HelloWorldController class (Controller). According to the configuration description in the preceding configuration file, the second URL segment should be the action (action/method), that is, the method name. Of course, you will ask me that I didn't access the Index method just now, why is the result a string of the index method not a string returned by the welcome method. Here, I will talk about it in my personal opinion. If this is not the case, I hope you can advise me! Because we didn't specify the action name (method) in the Controller for it, the routing mechanism will retrieve the default method of the field here, and the index method will be retrieved here. At the same time, this is why every time we create a Controller, it will automatically create a default action Method for us. This is perhaps one of its functions ...... Then we can continue to simulate our second method in the HTTP request to see if we can return the desired string. The request access result is as follows:
When we request the Welcome () method, we return the string we want to print to the page. At the same time, I want to explain that the two methods of the same HTTP request simulation are GET requests. Do you think that method is fast to execute?
Haha, of course, it is second. Here someone will ask why, because the second parameter specifies the Action name (method name). All routing mechanisms are directly searched based on the method name you provide, the First Routing Mechanism has a retrieval or check action, which may be slightly slower than the second method. Let's look at the information returned by the second staff tool as follows:
Whether the above problem is explained in the granularity details. This is purely a personal opinion.
We can slightly modify our welcome () method as follows:
Then we simulate the Http link and parameter request method to see if it can achieve our intended purpose. The operation is as follows:
Such a connection request is absolutely OK, but this person can see at a glance that I used the parameter to access the link, at this time, most people will say that you are too spam, too weak! Isn't it easy to get burst chrysanthemum! O (^) o actually I am absolutely so spam, things are naked in the address bar waiting for a burst of Chrysanthemum. What should I do now! Some friends will come up to make it simple. I write an encryption and decryption class to decrypt the encryption at the place where the parameter is obtained when passing the parameter. You can do this, of course, this cainiao has also done this kind of thing. However, we now use mvc, which can customize routing. In this case, we can configure a mechanism for such a URL, in the future, it will be better to follow our mechanism for URLs like this. Of course, I have mentioned this in my previous articles. If you are interested, you can flip it over.
We are now configuring such a routing mechanism in RouteConfig. configure the configuration in cs. Do not drag a class to configure it. You can configure it as needed. That is, you can find a way to let the routing mechanism know your configuration. We will go to RouteConfig. configure the Routing Mechanism in cs as follows:
To add such a routing mechanism, we will simulate our Welcome () method in the HTTP request again, which is roughly as follows:
Is this a much more beautiful request URL! However, our request is not the same as what we want. The value of 24 is 0. This is because we have a problem with the parameter in this welcome. Let's simply change it, modify the Code as follows:
Then we simulate the HTTP request. The request result is as follows:
OK. I will share it here for now today. o (^) o!