Asp.Net Core Web Application-Exploration

Time:2019-8-9

Preface

As a developer in Windows, I have almost no chance to use Core, but considering Microsoft’s strategic plan, I think Core still needs to know first.

Because Microsoft has developed two frameworks, one is the Net Framework and the other is the Net Core.

Almost all updates to new features are in the framework of Net Core.

So, considering the future, once Core is perfect, Microsoft will definitely abandon the. Net FrameWork.

Therefore, it must be a big trend for. Net programmers to switch to Net Core collectively.

So let’s look at Asp.Net Core Web applications in a spirit of exploration.

Create the Asp.Net Core project

First, let’s create an Asp.Net Core Web application project and then explore it together.

Open Visual Studio to create the project and select the Asp.Net Core Web application as follows:

 

Then select the Asp.Net Core Web application as follows:

Then we got such a layout project, as follows:

As you can see, the project has four files and two folders (Page, wwroot).

The wwwroot folder is very special, the icon is different from other folders, but it can still change its name. After changing the name, the folder icon will change back to the ordinary icon. But since it is a special icon, it must have special significance. We will study it later, and then browse down the Page folder.

When the Page folder expands, we find that there are many pages in it. So obviously, it is the place where the pages are stored. We will look at the contents of the pages later. Now, let’s first look at the four outermost files of the project.

Program.cs

I’m also surprised to see this file. The Web relies on IIS deployment. There is no Program in AspNet. Why is there an extra Program file in Core? Let’s investigate.

It turns out that AspNetCore has a built-in server called Kestrel.

What is a self-contained server? It’s like we created a WCF service, but we didn’t want to hang it on IIS, so we built a Service Host to hang it.

However, Kestrel is significantly more advanced, and it also supports the use of reverse proxy servers (such as Internet Information Services (IIS), Nginx or Apache).

What is a reverse proxy server? It is a server similar to IIS that receives HTTP requests from the network, forwards these requests to Kestrel, and finally implements the call by Kestrel. The call process is shown in the following figure.

By investigating here, I can roughly guess what Program. CS is doing – it should be used to start Kestrel.

Now I open Program. CS and find the following code.


public static IWebHost BuildWebHost(string[] args) =>
   WebHost.CreateDefaultBuilder(args)
    .UseStartup<Startup>()
    .Build();

Personally, I think this code is pitfalls. It’s a short version of a function, but it doesn’t play the role of shorthand. It’s also easy to disturb beginners. So let’s make some modifications, as follows:


public static void Main(string[] args)
{
 BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)
{
 return WebHost.CreateDefaultBuilder(args)
  .UseStartup<Startup>()
  .Build();
}

Looking at the code after the modification, we have made it clear that the Main function starts and calls the BuildWebHost function, so the name means that this is a function to create a website server, and the return value is IWebHost.

Then, we see that the Run method below is executed using the IWebHost instance returned by the BuildWebHost function in the Main function.

So far, it is clear that Program is used to start the server.

Startup.cs

This file is relatively familiar to us. It is a file that will be called when the project starts. It has many functions. Let’s look at the code generated by the system for us.


public class Startup
{
 public Startup(IConfiguration configuration)
 {
  Configuration = configuration;
 }
 public IConfiguration Configuration { get; }
 public void ConfigureServices(IServiceCollection services)
 {
  services.AddMvc();
 }
 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
 {
  if (env.IsDevelopment())
  {
   app.UseBrowserLink();
   app.UseDeveloperExceptionPage();
  }
  else
  {
   app.UseExceptionHandler("/Error");
  }
  app.UseStaticFiles();
  app.UseMvc();
 }
}

We’ve seen three functions. Now, let’s simply break the point for three functions and start the website.

Very simply, the sequence of the three functions is Startup – > ConfigureServices – > Configure.

The constructor is a simple assignment, so let’s skip it and look at ConfigureServices.

You can see that only services. AddMvc () is invoked in ConfigureServices to see the official introduction. The original method is to add Mvc services to the specified set of services.

Through debugging, we find that services. AddMvc () of ConfigureServices function is paired with the ConfigureFunction app. UseMvc (), that is, when we add MVC services to the service set, we can use Mvc services in the subsequent ConfigureMethod.

So we’re building Web applications. Why add Mvc services? Let’s delete the Mvc service and see how it works.

After deleting the Mvc service, we will find that the website started up, but did not visit our homepage normally.

Re-add back to Mvc service, we start the website again, check the link path of the website as follows:

http://localhost:1234/Indexp>

http://localhost:1234/About

It can be found that these paths are Mvc-mode paths, that is to say, Asp.Net Core Web applications also use Mvc routes to access web addresses, so Mvc services must be added.

In Configure, we see that other IApplication Builder methods are also used, but even if they are commented out, they will not affect the launching of the website, so we ignore them temporarily and use them in learning.

bundleconfig.json

So the name means, bundle configuration file, feel and mvc’s Bundle Config. CS file is very similar, open to see, you can confirm that is mvc’s bundle configuration file. That is to say, this is a useless file, because in most cases, we do not bundle configuration.

appsettings.json

Still the old name means the application settings file, which is very similar to the AppSetting node in webconfig, so infer that it should be a fixed value file for the configuration project.

Baidu appsettings. json, found that there are many articles about how to read the content of the file, so now you can be sure that it is a variable configuration file.

Now that the file is explained, let’s look at the contents of the folder.

wwwroot

As you mentioned, wwwroot is a folder with special markings.

Open wwwroot and we will find that style and image are stored in it. Running the website and looking at these pictures on the website, you will find that the address of the pictures is very strange.

The image path is / wwroot / images / banner1.svg, but accessed at http://localhost:1234/images/banner1.svg.

That is to say, the wwwroot path will be omitted, in other words, the wwroot will be placed in the root directory of the website.

We’re doing an experiment, creating a new folder to store some pictures, running website access, we will find that these pictures can’t be accessed at all.

So, we can conclude that wwroot is the only resource folder accessible by Asp.Net Core Web applications.

Pages

Opening the Page folder, we can see four expandable cshtml and four unexpandable cshtml files.

Open our most familiar _ViewStart.cshtml, double-click to enter, and find the code as follows:


@{
 Layout = "_Layout";
}

As you can see, the ViewStart code is the same as the MVC ViewStart, which means that this is a configuration layout file.

So the corresponding _Layout. cshtml can also be determined, it is a layout file, then, the remaining two cshtml files, we can also infer that they are also configuration files or layout files.

Let’s look at the four expandable cshtml files.

First, we expand the Index. cshtml file, as follows:

Next, we double-click the Index. cshtml file and find that it contains the normal HTML + razor tag.

Then, we double-click the Index. cshtml. CS file to see the content, and get the code as follows:


public class IndexModel : PageModel
{
 public void OnGet()
 {
 }
}

From the project structure, we can judge that Index. cshtml. CS is a background file of Index. cshtml.

But when you look at the code, you find that the class inside is an IndexModel that inherits the PageModel class. What does it really have to do with the Index. cshtml file?

Let’s first guess by naming that IndexModel contains the Model keyword, so it should be a Model related to the Index. cshtml file.

Model related to Index. cshtml file? That’s not ViewModel!!!

Now let’s go back and look carefully at the Index. cshtml file for clues.

I find that the first two lines of the document are as follows:


@page
@model IndexModel

This is how Mvc handles page entities, that is, IndexModel is indeed an entity of Index. cshtml.

So, our above conjecture is confirmed. The Index. cshtml. CS file is the ViewModel of the Index. cshtml file.

But what is Onget?

We’re still guessing by name, and I guess it’s the Page Load of AspNet before?

Let’s test it and modify the code as follows:


public string title; <br>public void OnGet()
{
 title = this.Request.Query["title"];
 if (!string.IsNullOrWhiteSpace(title))
 {
  ViewData["Title"] = title;
 }
}

Then the breakpoint Onget method.

Then we visit http://localhost:1234/index?Title=kiba.

As a result, our breakpoint was hit and the title was successfully set. Therefore, our speculation is successful again, OnGet is our previous PageLoad method.

epilogue
To sum up, we have a certain understanding of Asp.Net Core Web applications, and then I come to the conclusion that:

[Asp.Net Core Web Application] adopts the MVVM design concept (cshtml.cs file is the ViewModel of our [server] page). The request website uses Mvc’s path access technology, which is a better AspNet framework as a whole.

This is the end of Asp.Net Core Web application exploration.

The code has been uploaded to Github. You are welcome to download it.

Github address: https://github.com/kiba518/KibaAspNet Core

Above is the ASP.Net Core Web application introduced by Editor Xiaobian, which explores detailed integration and hopes to be helpful to you. If you have any questions, please leave a message for me, and Editor Xiaobian will reply to you in time. Thank you very much for your support to developpaer.