The foreword 0.

This is a new series called getting started with ASP.NET Core. This series will cover ASP.NET Core MVC and help with the basics of the front end (it can be used to implement what we need, but not the main one). This series will also cover other types of projects for the ASP.NET Core platform and lead you through developing a website or web service center based on each type of project.

Asp.net Core is supported by Asp.net Core

1. Introduction to ASP.NET Core

In this section I’ll take you through the evolution of ASP.NET Core.

At first, Microsoft developed ASP applications in order to replace CGI scripts. An Active Server Page is an Active Server Page. ASP is a combination of HTML+ server code, its suffix is. ASP.

In 2001, Microsoft announced the migration of ASP to. NET Framework, formed a new scripting language named asp.net. The first version was released in 2002, called ASP.NET 1.0, attached to the. NET Framework 1.0. Asp. Net is not only ASP +, but also ASP +.net. Asp.net is much more powerful when combined with.net.

Sometime before 2008, ASP.NET MVC went live. Since then, ASP.NET has been divided into two technologies: ASP.NET WebForm (formerly ASP.NET); Asp.net MVC. MVC greatly reduces the setting of a large number of server scripts in the front page. At the same time, MVC is closer to the HTML language, making the program development engineer and the front end can better cooperate.

In 2016, Microsoft will implement its cross-platform strategy. The NET Framework has been extracted. NET Core version, 1.0 released the same year. At the same time, asp.net core RTM was released, with the official release coming in 2017.

Net core, along with its smaller friends EF core and ASP.NET core, has since gone on to become what it is today.

Of course, the above content omitted a large number of plots, interested partners can consult relevant information. I’m not going to do too much here, because it’s too long, and it’s going to stray from the main line.

1.1 why ASP.NET Core

So why did we choose ASP.NET Core? At present, there are many industry systems and domain related systems on the market using ASP.NET Webform, the market share is still not small. But there is one thing we have to take into account and that is the first mover advantage and corner overtaking.

As it stands now, the old technology market is full and the new technology market is empty (of course, as I write this — ASP. Net Core is not new). If you want to learn a technology, the best place to do it is to learn the latest.

Moreover, ASP.NET Core supports cross-platform. .net was not cross-platform at first, but it was Mono who made the effort to make.net cross-platform. Later, Microsoft came up with the cross-platform strategy and the Internet of Things strategy, and the formal cross-platform implementation was put forward.

So, I think there’s a good reason.

1.2 为什么是MVC

MVC is short for model-view-controller, which means model-view-controller. Its communication flow looks like this:

  • User access interface (View), submit requests (including access requests)
  • The interface forwards the user’s request to the Controller and encapsulates it to some extent
  • After the Controller processes it and completes it, it returns it to the View as a Model
  • The View parses the returned Model and draws an interface to present to the user

MVC reduces the coupling between the page and the controller, simply by drastically reducing server scripting on the page. In addition, enhanced page reuse. ASP.NET Core MVC is further optimized in this convenience and reduces the number of tags that do not conform to HTML format.

Of course, MVC has additional benefits. The main point, though, is that MVC can get you one step closer to the big boys (meaning you can get a job).

2. Getting started with ASP.NET Core

In the last section, I made a lot of nonsense, which probably put off a lot of people. Haha, just kidding. Let’s get started with ASP.NET Core.

  1. Create a solution:

    dotnet new sln --name AspDemoCopy the code
  2. Create an MVC project and add it to the solution

    dotnet new mvc --name MvcWeb
    dotnet sln add MvcWebCopy the code

At this point, if nothing else, you should see the following directory structure in the AspDemo directory:

├ ─ ─ AspCoreDemo. SLNS └ detected ─ ─ MvcWeb ├ ─ ─ appsettings. Development. The json ├ ─ ─ appsettings. Json ├ ─ ─ Controllers │ └ ─ ─ HomeController. Cs ├ ─ ─ Models │ └ ─ ─ ErrorViewModel. Cs ├ ─ ─ MvcWeb. Csproj ├ ─ ─ obj │ ├ ─ ─ MvcWeb. Csproj. Nuget. Dgspec. Json │ ├ ─ ─ MvcWeb. Csproj. Nuget. G.p rops │ ├ ─ ─ MvcWeb. Csproj. Nuget. G.t argets │ ├ ─ ─ project. The assets. The json │ └ ─ ─ project. Nuget. Cache ├ ─ ─ the Program. The cs ├ ─ ─ the Properties │ └ ─ ─ launchSettings. Json ├ ─ ─ Startup. Cs ├ ─ ─ Views │ ├ ─ ─ Home │ │ ├ ─ ─ Index. The CSHTML │ │ └ ─ ─ Privacy. CSHTML │ ├ ─ ─ Shared │ │ ├ ─ ─ the Error. The CSHTML │ │ ├ ─ ─ _Layout. CSHTML │ │ └ ─ ─ _ValidationScriptsPartial. CSHTML │ ├ ─ ─ _ViewImports. CSHTML │ └ ─ ─ _ViewStart. CSHTML └ ─ ─ below ├ ─ ─ CSS │ └ ─ ─ at site. The CSS ├ ─ ─ the favicon. Ico ├ ─ ─ js │ └ ─ ─ site. Js └ ─ ─ Lib ├ ─ ─ the bootstrap │ ├ ─ ─ dist │ │ ├ ─ ─ CSS │ │ │ ├ ─ ─ the bootstrap. CSS │ │ │ ├ ─ ─ the bootstrap. CSS. The map │ │ │ ├ ─ ─ CSS │ │ ├─ Heavy Lifting exercises - - Heavy Lifting Exercises. CSS │ │ ├─ Heavy Lifting Exercises - - Heavy Lifting Exercises - - Heavy Lifting Exercises CSS │ │ ├─ Heavy Metal Exercises - Heavy metal Exercises - Heavy metal Exercises - Heavy metal Exercises - Heavy metal Exercises │ │ ├ ─ ─ the bootstrap - reboot. CSS. The map │ │ │ ├ ─ ─ the bootstrap - reboot. Min. CSS │ │ │ └ ─ ─ the bootstrap - reboot. Min. CSS. The map │ │ └ ─ ─ js │ │ ├── Heavy Metal Flag ── Heavy metal flag ── Heavy metal flag The bootstrap. Bundle. Min. Js. Map │ │ ├ ─ ─ the bootstrap, js │ │ ├ ─ ─ the bootstrap, js. Map │ │ ├ ─ ─ the bootstrap. Min. Js │ │ └ ─ ─ ├─ ├─ ├─ ├─ ├─ jquery.min.js │ ├─ ├─ jquery.min.js │ ├─ ├─ jquery.min.js │ ├─ ├─ jquery.min.js │ ├─ ├─ jquery.min.js │ ├─ ├─ jquery.min.js │ └ ─ ─ LICENSE. TXT ├ ─ ─ jquery - validation │ ├ ─ ─ dist │ │ ├ ─ ─ additional - the methods. Js │ │ ├ ─ ─ additional - the methods. Min. Js │ │ ├─ └─ jquery.validate.js │ ├─ jquery.validate.min.js │ ├─ jquery-validate-unobtrusive ├─ Jquery. Validate. Unobtrusive. Js ├ ─ ─ jquery. Validate. Unobtrusive. Min. Js └ ─ ─ LICENSE. TXTCopy the code

A brief introduction to several directories in the MvcWeb project:

  • Controllers hold the Controllers that handle the data returned by the view
  • Models holds the Model layer code, and the directory name is not required, nor does it have to be there
  • Views holds the view path, which is a fixed name
  • Wwwroot is used to store JS scripts and CSS stylesheets
  • The obj directory is a compiled directory, so don’t worry too much about it.

So, let’s run this project to see what it looks like:

CD MvcWeb dotnet run # or dotnet run --porject MvcWebCopy the code

The project is started and completed if the following information is displayed:

Then type in your browser:

http://localhost:5000Copy the code

You should then see the following:

At present, it is an empty project, don’t worry, we will continue to enrich this project in the next article of this series, so that its content will be richer and more suitable for our needs.

3. Program.cs

Does that name sound familiar? That’s right. We’ve been using console programs for every demo, and there’s a program. cs file with a Main method in it. As we know, the Main method is an entry point to a program. Previous Asp.net projects didn’t have this method because they were built on IIS. Asp.net core takes IIS out of the way to run it directly, so there’s an entry method.

The code should look like this:

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });
}Copy the code

3.1 Changing a Port

When we use, there will often be a port occupied, at this time we need to set the port. The setting method is as follows:

webBuilder.UseUrls("http://*:5006");Copy the code

Then restart the project to see that the port has changed.

4. Setup.cs

This class configures the request pipeline for the service and application. This is a convention name. The initial version of the class file should look like this:

public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id? } "); }); }}Copy the code

5. To summarize

This article takes you through the asp.net core MVC project. Arguably this is the project that best exemplifies Microsoft’s convention over configuration. MVC projects have very little configuration and inheritance. All methods and classes are distinguished by their agreed names and argument lists. So when you’re working on an MVC project and you don’t see how it looks as expected, look for spelling mistakes.

Well, that’s all for this one. The asp.net core series looks forward to your attention, and the next installment will show you how to write a controller and access it.

For more information, please check out my blog Mr. Gao’s Cabin.