当前位置:网站首页>Using asp Net core MVC framework for building web applications

Using asp Net core MVC framework for building web applications

2022-06-23 20:49:00 conanma

One 、 Preface

1、 Main contents of this paper

  • Use dotnet cli Create solutions based on (sln+csproj) Project
  • Use Visual Studio Code Development is based on solutions (sln+csproj) Project
  • Visual Studio Code Solution plug-in unit ( vscode-solution-explorer) Introduction to basic use
  • be based on .NET Core web Project template building ASP.NET Core MVC Web application
  • ASP.NET Core MVC Start with the frame

2、 Environment information for this tutorial

Software / Environmental Science

explain

operating system

Windows 10

SDK

2.1.401

ASP.NET Core

2.1.3

IDE

Visual Studio Code 1.27

browser

Chrome 69

3、 Pre knowledge

What you may need to know

  • MVC frame / Model is introduced

https://baike.baidu.com/item/mvc

  • Inversion of control (IOC) Principles and dependency injection (DI)

ASP.NET Core Integrated by default DI. All official module introductions will be used DI Way to introduce .

https://baike.baidu.com/item/IOC

Two 、 Project preparation

1、 Project creation

.NET There are two concepts of platform project construction : Solution (Solution)、 project (Project). All the project development , Whether it's Web project , Or console application , Must be based on Project To build . and Solution The function of Project Organize

If the project is simple , We just need to be based on Project To build a project , But when a project needs to be decoupled hierarchically , If we Project Creating directories to isolate doesn't work as hard isolation , After all, as long as it's in a Project You can quote . And by Project Layering can achieve the effect of hard isolation . And based on Project Code reuse is more concise and reasonable ( Compile output .dll It can be referenced in other projects, etc )

Solution (Solution)+ project (Project) It's equivalent to using Maven Built Java In the project , top floor Project and Project The relationship between .

  • Create project directory
# Create project directory 
mkdir Ken.Tutorial

# Enter project directory 
cd Ken.Tutorial
  • Create solution files
dotnet new sln -n Ken.Tutorial
  • establish Web project
dotnet new web -n Ken.Tutorial.Web
  • Add the project to the solution
dotnet sln add Ken.Tutorial.Web

2、VS Code To configure

  • The installation is based on Solution Development .NET Core Extension of the project

Extension

explain

vscode-solution-explorer

establish 、 Delete 、 Rename or move solutions 、 Solution folders and projects . Manage project references .

VS Code The extension management page directly searches for the extension to install , The version of this installation is :0.2.33

3、 ... and 、VS Code Develop solution based project descriptions

1、VS Code Project configuration

menu : file -> Open folder , Select the project directory to open the project

Because it's already installed VS Code Of C# Extension and Solution Expand , So it will also prompt that the relevant configuration is missing

C# Extension tips :

Required assets to build and debug are missing from ‘helloweb’. Add them?

ASP.NET Core Introductory tutorial

This is due to a lack of compilation for the project 、 Debug configuration , choice Yes that will do

vscode-solution-explorer Extension tips :

Would you like to create the vscode-solution-explorer templates folder?

ASP.NET Core Introductory tutorial

This is because vscode-solution-explorer The plug-in needs the corresponding template provided by the solution in the project .

Default configuration files for all plug-ins , It's all in .vscode In the folder

ASP.NET Core Introductory tutorial

In addition to the default panel in Explorer , We installed Solution Plug ins also provide friendly Solution Explorer. The style of this view , Yes VS(Visual Studio) Sense of being alike . The default Explorer can be completely hidden in subsequent project development , Use Solution Explorer Just fine .

2、Solution Explorer Menu Introduction

  • Solution Introduction to the right mouse button menu

ASP.NET Core Introductory tutorial

menu

Shortcut key

explain

Add existing project

/

Add an existing project (Project)

Add new project

/

New projects (Project)

Create folder

Ctrl+Shift+F

Create folder

Open File

/

Open the solution file (.sln)

Rename

F2

Modify the solution name

Build

/

Compiling solutions (Solution)

Clean

/

Clean up the solution (Solution) The compiled output of

Pack

/

Solution (Solution) pack

Publish

/

Publishing solutions (Solution)

Restore

/

Recovery solutions (Solution)

Test

/

Execute solution (Solution) Unit tests in

  • Project Introduction to the right mouse button menu

ASP.NET Core Introductory tutorial

menu

Shortcut key

explain

Add package

/

add to package

Add reference

/

Reference other projects in the solution

Create file

Ctrl+Shift+A

create a file

Create folder

Ctrl+Shift+F

Create folder

Move

/

Mobile projects (Project)

Remove project from solution

Del

Remove the project from the solution (Project)

Paste

Ctrl+V

Paste

Open File

/

Open project file (.csproj)

Rename

F2

Modify the solution name

Build

/

Compile the project (Project)

Clean

/

Clean up the project (Project) The compiled output of

Pack

/

project (Project) pack

Publish

/

Publish the project (Project)

Restore

/

Recovery projects (Project)

Test

/

Perform project (Project) Unit tests in

Four 、ASP.NET Core MVC Output HelloWorld

1、 introduce ASP.NET Core MVC

Modify the application startup class (Startup.cs), introduce MVC Module and configure the default route

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // introduce MVC modular 
        services.AddMvc();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseMvc(routes =>
        {
            // Configure the default route 
            routes.MapRoute(
                name: "Default",
                template: "{controller}/{action}",
                defaults: new { controller = "Home", action = "Index" }
            );
        });
    }
}

2、 establish Controller And Action

  • establish HomeController

stay Ken.Tutorial.Web Add a folder to the project :Controllers, And create a class in the folder HomeController Type selection is :class

using System;

namespace Ken.Tutorial.Web.Controllers
{
    public class HomeController
    {
    }
}
  • quote MVC Namespace , And inherit with Controller
using System;
using Microsoft.AspNetCore.Mvc;

namespace Ken.Tutorial.Web.Controllers
{
    public class HomeController : Controller
    {
    }
}

ControllerName=Home

  • Definition Action:Index
using System;
using Microsoft.AspNetCore.Mvc;

namespace Ken.Tutorial.Web.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return Content("Hello World!");
        }
    }
}

ActionName=Index

3、 Project startup and access testing

  • Modify protocol and port

modify Ken.Tutorial.Web project Properties In the folder launchSettings.json file , Use HTTP Protocol and listen port 5001

"Ken.Tutorial.Web": {
  "commandName": "Project",
  "launchBrowser": true,
  "applicationUrl": "http://localhost:5001",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
  • Start project

Press down F5 Start project , After the project starts successfully ,VS Code Will help us open the default browser and visit :http://localhost:5001

ASP.NET Core Introductory tutorial

The reason why HomeController in Index(Action) The return content of , It's because we defined the default route earlier, which can be from {controller}/{action} The access path corresponds to Action, And we define the default value : controller = "Home", action = "Index"

routes.MapRoute(
    name: "Default",
    template: "{controller}/{action}",
    defaults: new { controller = "Home", action = "Index" }
);

We can also pass http://localhost:5001/home/index Show the access

5、 ... and 、ASP.NET Core View base uses

1、 Create return View Of Action

stay HomeController add to Action:Time

public IActionResult Time()
{
    // Put the current server time into ViewBag in 
    ViewBag.ServerTime = DateTime.Now;
    return View("Time");
}

2、 Create view file

Create a folder in the project Views, And create the corresponding HomeController View subfolders :Home. The reason for creating a folder like this , Because when we go back to the view , Specify only ViewName, Without specifying the full path .ASP.NET Core MVC By default, the framework reads the view files in the following project directory in turn :

  • /Views/{ControllerName}
  • /Views/Shared
  • /Pages/Shared

If you find the view file, you render the view , If not, an exception will be thrown .

Create view file /Views/Home/Time.cshtml

@ViewBag.ServerTime  -ken.io

View rendering @ViewBag.ServerTime Will be output Action The content of the assignment in , -ken.io Will be rendered as a string

3、 Start project testing

Press down F5 Start project , After the project starts successfully, enter... In the browser http://localhost:5001/home/time And access , You will see the following output :

原网站

版权声明
本文为[conanma]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/12/202112291149322544.html