zoukankan      html  css  js  c++  java
  • ASP.NET Core 入门笔记2,建立项目

    1.建立项目

    2.项目结构

    1、项目结构说明

    根目录/文件说明
    .vscode目录 VS Code项目配置目录,相当于.vs、.idea文件夹
    bin目录 编译输出目录
    obj目录 编译配置与中间目录,用于存放编译配置与编译中间结果
    Properties目录 用于存放项目配置
    wwwroot目录 静态文件目录,css,js,图片等文件
    helloweb.csproj文件 项目描述文件
    Program.cs文件 应用程序入口类文件
    Startup.cs文件 ASP.NET Core Web应用启动类文件,用于项目启动前进行相关配置

    3.启动项目

    我们直接按下F5,或者菜单:调试->启动调试启动项目,这里我们直接选择应用程序而不是IIS Express
    ASP.NET Core 默认绑定是5001端口,而且ASP.NET Core 2.1之后默认绑定了HTTPS,项目启动成功后,VS Code会帮我们打开默认浏览器并访问:https://localhost:5001

    4.修改绑定协议HTTPS为HTTP,并且修改配置文件

    接着我们可以修改配置去掉HTTPS协议绑定
    打开Properties/launchSettings.json文件

    {
      "iisSettings": {
        "windowsAuthentication": false, 
        "anonymousAuthentication": true, 
        "iisExpress": {
          "applicationUrl": "http://localhost:6323",
          "sslPort": 44330
        }
      },
      "profiles": {
        "IIS Express": {
          "commandName": "IISExpress",
          "launchBrowser": true,
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        },
        "Demo00BuildProject": {
          "commandName": "Project",
          "launchBrowser": true,
          "applicationUrl": "https://localhost:5001;http://localhost:5000",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        }
      }
    }

    iisSettings、profiles.helloweb配置节点都有启动绑定配置,因为VS Code启动项目默认是不通过IIS来host的,iisSettings选项我们忽略即可。将applicationUrl修改为http://localhost:5001
    然后重启项目(Ctrl+Shift+F5)机会看到干净纯洁的Hello World!

    {
      "profiles": {
        "Demo00BuildProject": {
          "commandName": "Project",
          "launchBrowser": true,
          "applicationUrl": "http://localhost:5001",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        }
      }
    }

    修改后运行

    4.项目启动简介

    应用程序入口类,在应用启动的时候,会执行CreateWebHostBuilder方法,在这个方法中通过类Startup创建了默认了HostBuilder

        public class Program
        {
            public static void Main(string[] args)
            {
                CreateWebHostBuilder(args).Build().Run();
            }
    
            public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
                WebHost.CreateDefaultBuilder(args)
                    .UseStartup<Startup>();
        }

    应用启动类,web项目模板默认在项目启动的时候调用IApplicationBuilder.run方法,在当前HTTP上下文(HttpContext)中输出了Hello World!,ConfigureService一般用于启动单例,配置,注入等过程,Configure时用户请求管道,用于解析处理用户的请求,mvc中间件,权限控制等过程在此处进行

    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)
            {
    
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
    
                app.Run(async (context) =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            }
        }
    方法说明
    ConfigureServices 用于配置应用启动时加载的Service
    Configure 用于配置HTTP请求管道

    web项目模板默认在项目启动的时候调用IApplicationBuilder.run方法,在当前HTTP上下文(HttpContext)中输出了Hello World!

    context.Response.WriteAsync(“Hello World!”);

  • 相关阅读:
    JavaScript 数组操作函数--转载+格式整理
    Python之__str__类的特殊方法
    Django 模板层(Template)
    jquery基础
    Django基础(web框架)
    前端基础之JavaScript对象
    前端基础之JavaScript
    MySQL数据库之索引
    MySQL数据库之多表查询
    MySQL 数据库之单表查询
  • 原文地址:https://www.cnblogs.com/xiaoahui/p/11723752.html
Copyright © 2011-2022 走看看