zoukankan      html  css  js  c++  java
  • 01.Asp.Net Core 3.x 笔记 创建项目

    创建项目

    在VS中创建一个空的Asp.Net Core 3.1 Web应用程序

    Program.cs

        public class Program
        {
            public static void Main(string[] args)
            {
                CreateHostBuilder(args).Build().Run();
            }
    
            public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseStartup<Startup>();
                    });
        }
    
    

    Startup.cs

        public class Startup
        {
            //注册服务
            public void ConfigureServices(IServiceCollection services)
            {
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            //配置Http请求管道(即:添加配置中间件)
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
    
                app.UseRouting();
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapGet("/", async context =>
                    {
                        await context.Response.WriteAsync("Hello World!");
                    });
                });
            }
        }
    

    启动方式

    launchSettings.json

    {
      "iisSettings": {
        "windowsAuthentication": false, 
        "anonymousAuthentication": true, 
        "iisExpress": {
          "applicationUrl": "http://localhost:4982",
          "sslPort": 44314
        }
      },
      "profiles": {
        "IIS Express": {
          "commandName": "IISExpress",
          "launchBrowser": true,
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        },
        "Three": { //自己宿主,作为控制台应用启动
          "commandName": "Project",
          "launchBrowser": true,
          "applicationUrl": "https://localhost:5001;http://localhost:5000",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        }
      }
    }
    
    

    注册服务

    Startup.cs

            //注册服务
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddControllersWithViews(); //做Web 应用程序,使用视图页面
                services.AddControllers(); //做webApi应用
            }
    

    依赖注入自己的服务

    新建服务接口IClock,及其实现类ChinaClockUtcClock
    新建文件夹 Controllers,并在其里面新建文件 HomeController 继承 Controller

        public class HomeController : Controller
        {
            public HomeController(IClock clock)
            {
    
            }
        }
    

    注册自己的服务

            //注册服务
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddControllersWithViews(); //做Web 应用程序,使用视图页面  services.AddControllers(); //做webApi应用
                services.AddSingleton<IClock, ChinaClock>(); //添加自己的服务
            }
    
    
  • 相关阅读:
    除了被动阅读,用户想要的更多
    腾讯:建造“通天塔”的“帝企鹅”
    网吧里的互联网,在改变Martin和Paul的生活
    “两化融合”物联网应用和架构
    中文信息匮乏年代,新媒体粉墨登场
    互联网天生本质与产业大局的开始
    extjs 4 tree 的text不显示
    一个网上找到的,在Grid中嵌套Grid的示例:Nested Grids Example
    如何编写一个使用Store更新复选框的CheckboxGroup的插件
    Ext JS 4.2.0发布
  • 原文地址:https://www.cnblogs.com/easy5weikai/p/12992149.html
Copyright © 2011-2022 走看看