当前位置:网站首页>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 :
边栏推荐
- Do you need a server to set up cloud on demand? What services can cloud on demand provide?
- SQL聯合查詢(內聯、左聯、右聯、全聯)的語法
- [golang] quick review guide quickreview (x) -- goroutine pool
- OHOS LTS 3.0移植到RaspberryPi 4B
- What cloud disk types does Tencent cloud provide? What are the characteristics of cloud disk service?
- [golang] how to clear slices gracefully
- Application of MySQL time function, simple problem
- How to make a commodity price tag
- After the collapse of UST, will the stable currency market pattern usher in new opportunities?
- Is it safe for flush to open an account online? Is the Commission high
猜你喜欢

JS advanced programming version 4: generator learning

Kubernetes resource topology aware scheduling optimization

3000 frame animation illustrating why MySQL needs binlog, redo log and undo log

Technology sharing | wvp+zlmediakit realizes streaming playback of camera gb28181

Ugeek's theory 𞓜 application and design of observable hyperfusion storage system

Crise de 35 ans? Le volume intérieur est devenu synonyme de programmeur...

Implementation of microblog system based on SSM

After the collapse of UST, will the stable currency market pattern usher in new opportunities?

35 year old crisis? It has become a synonym for programmers

Syntax of SQL union query (inline, left, right, and full)
随机推荐
【Golang】使用Go语言操作etcd——配置中心
Fortress deployment server setup operation guide for novices
【Golang】快速复习指南QuickReview(七)——interface
[golang] type conversion summary
[vernacular technology] QR code
What is the role of computer auto audit audio? What content failed to pass the audit?
Applet development framework recommendation
Kubernetes resource topology aware scheduling optimization
FPGA based electromagnetic ultrasonic pulse compression detection system paper + source file
Teach you how to develop desktop applications with web pages
Development and code analysis of easycvr national standard user defined streaming address function
Dart series: your site is up to you. Use extension to extend classes
Add two factor authentication, not afraid of password disclosure, let alone 123456
Is Huishang futures trading software formal? How to download safely?
Memory patch amsi bypass
[golang] quick review guide quickreview (VII) -- Interface
【Golang】快速复习指南QuickReview(二)——切片slice
【Golang】快速复习指南QuickReview(三)——map
【Golang】在Go语言的角度重新审视闭包
How to solve the problem that the ID is not displayed when easycvr edits the national standard channel?