Core下简易WebApi
代码很粗糙~
粘贴github地址
https://github.com/htrlq/MiniAspNetCoreMini
demo
1 public class Startup 2 { 3 public Startup(IConfiguration configuration) 4 { 5 Configuration = configuration; 6 } 7 8 public IConfiguration Configuration { get; } 9 10 // This method gets called by the runtime. Use this method to add services to the container. 11 public void ConfigureServices(IServiceCollection services) 12 { 13 services.AddMvc(); 14 15 services 16 .AddApiServerInit() 17 .AddApiServer<MyController>(); 18 } 19 20 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 21 public void Configure(IApplicationBuilder app, IHostingEnvironment env) 22 { 23 if (env.IsDevelopment()) 24 { 25 app.UseDeveloperExceptionPage(); 26 } 27 28 app.UseMvc(); 29 30 app.UseRouter(route => 31 { 32 route.UserApiServer(); 33 }); 34 } 35 } 36 37 public enum OrderSort 38 { 39 DoExecute, 40 Success, 41 NotPay 42 } 43 44 public class ParamOrderSort 45 { 46 [Required] 47 public Guid? UserId { get; set; } 48 [Required] 49 public OrderSort? Type { get; set; } 50 public int? PageIndex { get; set; } 51 public int? PageSize { get; set; } 52 } 53 54 public class MyController : ApiServerBase 55 { 56 [HttpGet] 57 [HttpPost] 58 public async Task Show(ParamOrderSort param) 59 { 60 await context.HttpContext.Response.WriteAsync($"{param.UserId} {param.Type} {param.PageIndex} {param.PageSize}"); 61 } 62 63 public MyController(IHttpContextAccessor context) : base(context) 64 { 65 } 66 }