• NetCore的控制台应用中搭建WebServer的方法


    一、新建NetCore控制台项目,并引入下列Nuget包:

      Microsoft.AspNetCore.StaticFiles、Microsoft.AspNetCore.Http、Microsoft.AspNetCore.Http.Abstractions、Microsoft.AspNetCore.Server.Kestrel

      

    二、新建一个Startup类。

    using System;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Http;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;
    using Microsoft.Extensions.FileProviders;
    
    namespace NetCoreWebServerDemo
    {
        public class Startup
        {
            public void ConfigureServices(IServiceCollection services)
            {
            }
            public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
            {
                var staticfile = new StaticFileOptions();
                staticfile.FileProvider = new PhysicalFileProvider(@"D:");
                app.UseStaticFiles(staticfile);
                app.Run(new Microsoft.AspNetCore.Http.RequestDelegate(HttpRequestHandler));
            }
    
            private async Task HttpRequestHandler(HttpContext context)
            {
                await context.Response.WriteAsync("hello  here is songxingzhu prov.");
            }
        }
    }

    三、在Main函数中这样写:

    using Microsoft.AspNetCore.Hosting;
    using System.IO;
    
    namespace NetCoreWebServerDemo
    {
        class Program
        {
            public static void Main(string[] args)
            {
                var host =
                    new WebHostBuilder().
                    UseKestrel().
                    UseContentRoot(Directory.GetCurrentDirectory()).
                    UseUrls("http://0.0.0.0:8080").
                    UseStartup<Startup>().Build();
    
                host.Run();
            }
    
          
        }
    }

    四、启动运行。如图,一个简单的文件服务器就搭好了。

  • 相关阅读:
    kotlin中值范围
    kotlin中集合
    kotlin数据解构
    Java 8 Lambda 表达式
    kotlin 之内联函数
    kotlin之函数的范围和泛型函数
    kotlin函数的参数和返回值
    kotlin 之单表达式函数
    kotlin使用中辍标记法调用函数
    kotlin之函数的基本用法
  • 原文地址:https://www.cnblogs.com/songxingzhu/p/6814012.html
Copyright © 2020-2023  润新知