Core下简易WebApi

Posted 沉迷代码的萌新

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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     }

 

以上是关于Core下简易WebApi的主要内容,如果未能解决你的问题,请参考以下文章

.Net Core WebAPI CORS 与 Windows 身份验证

获取 ASP .Net Core WebAPI 中的所有身份角色

.Net Core 下

搭建一个简易框架 3秒创建一个WebApi接口

将 RabbitMQ 与 .NET Core WebAPI 一起使用时丢失消息

.Net Core Mvc/WebApi 返回结果封装