跳转至

建立项目

  1. 使用VS 2017 建立 web 项目

  2. 文件 > 新建 > 项目 > .NETCore > ASP.NET Core Web 应用程序

  3. 项目结构说明

  4. Properties 配置文件

    json { "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:50956", "sslPort": 0 } }, "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "WebApplication_Test": { "commandName": "Project", "launchBrowser": true, "applicationUrl": "http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } }

  5. wwwroot

    web 根目录

  6. Program.cs 程序入口

    ```c# namespace WebApplication_Test { public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); // 创建Web主机 }

          public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
              WebHost.CreateDefaultBuilder(args)
                  .UseStartup<Startup>(); //使用Startup类启动
      }
    

    } ```

  7. Startup.cs

    ```c# namespace WebApplication_Test { public class Startup { // 运行时调用此方法。使用此方法向容器添加服务。 public void ConfigureServices(IServiceCollection services) { }

          // 此方法由运行时调用。使用此方法配置HTTP请求管道。
          public void Configure(IApplicationBuilder app, IHostingEnvironment env)
          {
              if (env.IsDevelopment())
              {
                  app.UseDeveloperExceptionPage();
              }
    
              app.Run(async (context) =>
              {
                  await context.Response.WriteAsync("Hello World!"); // 无论收到任何http请求,都返回此响应
              });
          }
      }
    

    ```

Startup 里的配置

Controller

Model

Razor View

前端