-
Create the project development tool: VS2019
To create the WebMVC project, enter the keyword Core in the template input box, then go to the ASP.NET Core Web application below and select it, then click next, as shown below:
Then enter the project name and solution name and click Create, as shown below:
Next, select the “Empty” option, uncheck “Configure for HTTPS”, and click Create, as shown below:
The created project folder is as follows:
Next, create a folder called wwwroot, Controllers, Models, and Views. Wwwroot is used to store JS, CSS, or plug-ins. Controllers are used to store MVC controller files. Models are primarily used to store entity class files (for demonstration purposes, it is best to create all entity files in a separate library in a formal environment); Views stores view files generated by controllers.
At this point, the project is created using VS2019.
-
Edit item
2.1. Create the _layout. CSHTML view file
Create a _layout. CSHTML view file in the Shared folder of the Views folder. This file will be used as a master page for other view files where RenderBody will render and load the content of other child Views.
2.2. Create the _viewstart. CSHTML view file
Create the _viewstart. CSHTML view file in the Views folder. This file must be placed in the Views folder, as specified by Microsoft. _viewstart. CSHTML is the startup file of the View in the MVC framework, which is required in the MVC framework. You can specify the path of the master page _layout. CSHTML here.
Create the _ViewImports. CSHTML view file
Create the _ViewImports. CSHTML view file in the Views folder. The main purpose of this view file is to introduce the namespaces we need to use so that we don’t have to make separate references to each view file, and then write the system namespace to this file.
2.4. Create the homecontroller.cs controller file
Create the homecontroller. cs controller file in the Controllers folder and create the view file corresponding to Index.
Modify the startup. cs file as follows:
public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { / / registered MVC service 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(); } app.UseRouting(); / / registered MVC middleware, and configure the routing rules app. UseEndpoints (endpoints = > {endpoints. MapControllerRoute (name: "default", the pattern: "{controller=Home}/{action=Index}/{id? } "); }); }}Copy the code
2.6. Run the project
Ctrl+F5 to run the MVC project, as shown below:
The MVC project directory structure of Net Core is shown below: