ASP. MVC6 Departure (one)

Source: Internet
Author: User

The net MVC6 has been baked for a long time, but now many projects are still using MVC4, so MVC6 not yet to the fire, but from the big news newspapers in the media can more or less know that it is capable of cross-platform, This also makes up for the shortcomings of the ASP, so I also began to join the study of MVC6 camp, the study suddenly found that there are many familiar places, there are many whimper applauded places. I'll start with the problems I encountered when I first approached MVC6, documenting some of the basic applications of MVC6.

1. Open VS2015 to create an ASP. NET WEB Application project.

Figure 1

Then select the "Empty" template under "ASP." Why not choose "Web application" on the right? The reason is simple, learn as new things have to start from scratch, if the beginning of the IDE blinded by the eyes, how to move back?

Figure 2

After the project is created, we will see the following open files:

Figure 3

Let's start with what they mean, run a little bit and see what the effect is:

Figure 4

The result is this, then this "Hello world! "Where is it defined, obviously it is defined in line 27th of Figure 3, as long as we get rid of that place's string, the page will output different content." This means that the "Configure" method is called when our program is started, and then who calls it? The 32nd line of Figure 3 is what we want to find the main method, it is the entry of the program, as to its internal implementation mechanism, we have to go deep into the source to study, in order not to create frustration at the beginning, research source of the matter put back, first to achieve a simple small application, find a sense of accomplishment. All we need to know here is that it's for launching our web app.

Let's take a look at the structure of the MVC6 project (on the left), and I'll compare it with the MVC4 structure (right):

Figure 5 Figure 6

I was surprised to find that MVC6 's project doesn't seem to have a folder, so is that really the case? In fact, we can open the "wwwroot" This node, which appeared in our familiar "Web. config" file, that is, "wwwroot" is the root directory of the site, some of our static resources (CSS, JS, image, etc.) should be placed in this directory. So where did our "Controllers" folder and "views" folder go? No, it's not easy to build a chant by yourself. So we're going to build these two folders and put them:

Figure 7

Note: These folders should be built at the outermost layer of the project and should not be built under the "Wwwroot" folder. This means that the two folders should be at the same level as "wwwroot".

Then we right-click on the "Controllers" folder, then "Add"-"New Item", in the popup dialog, select "Server-side" on the left navigation to select "Server Side", then select "MVC" from the list that appears on the right. Controller Class, and then enter the name of the controller to add.

Figure 8

When the add is complete, VS automatically opens the controller file you just created, and the result is this:

Figure 9?

Ah! Why is there a wavy red line! Because the "Controller" class is not parsed, in other words, we have not yet referenced the MVC package. There are two ways to refer to the MVC package, which one likes to use:

The first: Add through the "Project.json" file under the project root directory. Open the file, we will see the JSON format of the data, we find the "dependencies" node, which defines the project depends on the package. We do the following additions:

Fig. 10 Fig. 11

So, after adding it, it should look something like this:

 "dependencies"  : { 
Span style= "' color:black '" > "Microsoft.AspNet.IISPlatformHandler" Span style= "' Color:black '" >: "1.0.0-rc1-final" ,
"Microsoft.AspNet.Server.Kestrel" Span style= "' Color:black '" >: "1.0.0-rc1-final" ,

The second type: through the Package Manager. First, right-click on the project's "References" node, tap "Manage NuGet Packages ...", then select "Browse" at the top of the tab that opens on the right, then enter "MVC" in the search box above, and then you'll find a lot of lists, Then we click to select the "MICROSOFT.ASPNET.MVC" item, while the right side of the property window will show the available version, when we select the version, click on the right "Install" button installed.

Figure 20

After the installation, let's look at the controller file has not been resolved to:

Figure 12

We found that it was very well parsed, and then we created a view just like the view created in MVC4. As follows:

Figure 13

Then we can't wait to run, and the result is this:

Figure 14

Why is "Hello world!", my "OK"? Someone may think that I did not put the address complete, OK, I hit the full look:

Figure 15

The result is still this, why is it? Let's look at the following code:

app. Run (Async  (context) =
await context. Response.writeasync ("Hello world!" );

The output of our several times is carried out by it, then what is it? Complex I do not say, now we can understand it as an example of implementing the "IHttpAsyncHandler" interface, in which the delegate is "ProcessRequest (HttpContext context)" method, this is not a little bit clear? Then "app." Run (...) " And what does it do again? We can simply learn by smart hints:

Figure 16

Smart hints say that this method adds a terminal middleware to handle the requests inside the application pipeline. What does that mean? We can use the following diagram to illustrate briefly:

Figure 17

So the output will always be "Hello world!" if it is not intercepted between IIS and the terminal middleware. So, how do we intercept it?

We can add a terminal middleware before it:

app. Run ( async   context =>  
await Span style= "' color:black '" > task . Run (() = {});

app. Run ( async (context) =>
await Span style= "' color:black '" > context. Response.writeasync ( "Hello world!" );

In this way, nothing will be output. However, it doesn't make sense to do so, how do we add MVC middleware? Very simply, we can add the MVC middleware by simply adding the following code in front of it. The inside of the route parameters I will not say more, used MVC4 should know.

App. Usemvc (route =
Route. MapRoute (
"Default" ,
"{controller}/{action}" ,
New"Home""index" }

Next, let's run for a second to see if it's successful:

Figure 18

Error, said to find the service, to ISERVICECOLLECTION.ADDMVC () to add services, So we look in the Startup.cs file, the result is a configuration iservicecollection place, and then we add the MVC service, as follows:

public void configureservices (iservicecollection  Services)  
Services. Addmvc ();

OK, then rebuild refresh the page:

Figure 19

The results found OK. Is there a little excitement, as for services. Addmvc () and apps. USEMVC (...) What is the relationship between the layout page, and the data access I'll talk about it next time.

ASP. MVC6 Departure (one)

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: 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.