.Net Core WebApi

  • .Net Core WebApi已关闭评论
  • 111 次浏览
  • A+
所属分类:.NET技术

MiniMalAPi

  • 最小的api, 请求都写在Program.cs中, 可以做微服务

Demo

Program.cs

//基本请求 app.MapGet("/GetTest", () => new { result = "123", code = 200 }).WithTags("General Request"); app.MapPost("/PostTest", () => new { result = "123", code = 200 }).WithTags("General Request"); app.MapPut("/PutTest", () => new { result = "123", code = 200 }).WithTags("General Request"); app.MapDelete("DeletePut", () => new { result = "123", code = 200 }).WithTags("General Request");  //注入 app.MapGet("/GetTestIoc", (IStudent student,IWrite pen) => student.Calligraphy(pen)).WithTags("Ioc"); app.MapGet("/GetTestIocById", (IStudent student,IWrite pen,int? id) => student.Calligraphy(pen)).WithTags("Ioc"); app.MapPost("/GetTestIocByObject", (IStudent student,IWrite pen,Result result) => student.Calligraphy(pen)).WithTags("Ioc"); 

Swagger

文档+信息

Program.cs

builder.Services.AddSwaggerGen(setup => {     setup.SwaggerDoc("V1", new()     {         Title = "qfccc",         Version = "V1",         Description = "qfccc description",     }); });  app.UseSwaggerUI(setup => {     setup.SwaggerEndpoint("/swagger/V1/swagger.json", "V1"); }); 

API版本控制

  • 该例子仅供参考

ApiVersion.cs

namespace WebApiDemo.VersionControl {     public class ApiVersion     {         public static string? Version1;         public static string? Version2;         public static string? Version3;         public static string? Version4;         public static string? Version5;     } } 

Version1Controller.cs

  • 这里其他版本api 修改ApiVersion.Version1即可

using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using WebApiDemo.VersionControl;  namespace WebApiDemo.Controllers {     [ApiExplorerSettings(GroupName = nameof(ApiVersion.Version1))]     [Route($"api/[controller]/[action]/api.{nameof(ApiVersion.Version1)}")]     [ApiController]     public class Version1Controller : ControllerBase     {          [HttpGet]         public IActionResult GetString() => new JsonResult(new { result = "Success" });          [HttpPost]         public IActionResult PostString() => new JsonResult(new { result = "Success" });          [HttpPut]         public IActionResult PutString() => new JsonResult(new { result = "Success" });          [HttpDelete]         public IActionResult DeleteString() => new JsonResult(new { result = "Success" });     } } 

Program.cs

builder.Services.AddSwaggerGen(setup => {     foreach (var field in typeof(ApiVersion).GetFields())     {         setup.SwaggerDoc(field.Name, new()         {             Title = "qfccc",             Version = field.Name,             Description = $"qfccc api {field.Name}",         });     } });  app.UseSwaggerUI(setup => {     foreach (var field in typeof(ApiVersion).GetFields())     {          setup.SwaggerEndpoint($"/swagger/{field.Name}/swagger.json", field.Name);     } }); 

生成注释

  1. 项目点击右键 -> 属性 -> 输出 -> 勾选(文档文件)
  2. 在AddSwaggerGen中间件添加下方代码

string? basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location); if(basePath is not null) {     string apiPath = Path.Combine(basePath, "项目名称.xml");     setup.IncludeXmlComments(apiPath); } 

解决跨域

  1. 调用方调用自己后台,然后用后台请求目标接口解决跨域
  2. 服务器支持跨域请求
    • 在Action中添加这行代码:
    • HttpContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
  3. 使用ActionFilter 实现过滤器,代码与上方一致, 然后在Program.cs 文件中全局注册一下该过滤器,但是不支持Post,Put,Delete这种请求
  4. 所有请求都支持跨域可以在Program.cs中增加下方代码:

builder.Services.AddCors( setup => {     setup.AddPolicy("SolveCrossOrigin", policy =>     {         policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin();     }); });   app.UseCors("SolveCrossOrigin"); 

.Net 后台请求封装

using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Net.Http; using System.Text; using System.Threading.Tasks;  namespace NET5WebApplication.Utility {     public static class HttpClientHelper     {         /// <summary>         /// 发起POST同步请求         /// </summary>         /// <param name="url"></param>         /// <param name="postData"></param>         /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>         /// <param name="headers">填充消息头</param>         /// <returns></returns>         public static string HttpPost(string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary<string, string> headers = null)         {             postData = postData ?? "";             using (HttpClient client = new System.Net.Http.HttpClient())             {                 if (headers != null)                 {                     foreach (var header in headers)                         client.DefaultRequestHeaders.Add(header.Key, header.Value);                 }                 using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))                 {                     if (contentType != null)                         httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);                     HttpResponseMessage response = client.PostAsync(url, httpContent).Result;                     return response.Content.ReadAsStringAsync().Result;                 }             }         }          /// <summary>         /// 发起POST异步请求         /// </summary>         /// <param name="url"></param>         /// <param name="postData"></param>         /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>         /// <param name="headers">填充消息头</param>         /// <returns></returns>         public static async Task<string> HttpPostAsync(string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary<string, string> headers = null)         {             postData = postData ?? "";             using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())             {                 client.Timeout = new TimeSpan(0, 0, timeOut);                 if (headers != null)                 {                     foreach (var header in headers)                         client.DefaultRequestHeaders.Add(header.Key, header.Value);                 }                 using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))                 {                     if (contentType != null)                         httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);                      HttpResponseMessage response = await client.PostAsync(url, httpContent);                     return await response.Content.ReadAsStringAsync();                 }             }         }          /// <summary>         /// 发起GET同步请求         /// </summary>         /// <param name="url"></param>         /// <param name="headers"></param>         /// <param name="contentType"></param>         /// <returns></returns>         public static string HttpGet(string url, string contentType = "application/json", Dictionary<string, string> headers = null)         {             using (HttpClient client = new HttpClient())             {                 if (contentType != null)                     client.DefaultRequestHeaders.Add("ContentType", contentType);                 if (headers != null)                 {                     foreach (var header in headers)                         client.DefaultRequestHeaders.Add(header.Key, header.Value);                 }                 HttpResponseMessage response = client.GetAsync(url).Result;                 return response.Content.ReadAsStringAsync().Result;             }         }          /// <summary>         /// 发起GET异步请求         /// </summary>         /// <param name="url"></param>         /// <param name="headers"></param>         /// <param name="contentType"></param>         /// <returns></returns>         public static async Task<string> HttpGetAsync(string url, string contentType = "application/json", Dictionary<string, string> headers = null)         {             using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())             {                 if (contentType != null)                     client.DefaultRequestHeaders.Add("ContentType", contentType);                 if (headers != null)                 {                     foreach (var header in headers)                         client.DefaultRequestHeaders.Add(header.Key, header.Value);                 }                 HttpResponseMessage response = await client.GetAsync(url);                 return await response.Content.ReadAsStringAsync();             }         }          /// <summary>         /// 发起POST同步请求         /// </summary>         /// <param name="url"></param>         /// <param name="postData"></param>         /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>         /// <param name="headers">填充消息头</param>         /// <returns></returns>         public static T HttpPost<T>(string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary<string, string> headers = null)         {             return HttpPost(url, postData, contentType, timeOut, headers).ToEntity<T>();         }          /// <summary>         /// 发起POST异步请求         /// </summary>         /// <param name="url"></param>         /// <param name="postData"></param>         /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>         /// <param name="headers">填充消息头</param>         /// <returns></returns>         public static async Task<T> HttpPostAsync<T>(string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary<string, string> headers = null)         {             var res = await HttpPostAsync(url, postData, contentType, timeOut, headers);             return res.ToEntity<T>();         }          /// <summary>         /// 发起GET同步请求         /// </summary>         /// <param name="url"></param>         /// <param name="headers"></param>         /// <param name="contentType"></param>         /// <returns></returns>         public static T HttpGet<T>(string url, string contentType = "application/json", Dictionary<string, string> headers = null)         {             return HttpGet(url, contentType, headers).ToEntity<T>();         }          /// <summary>         /// 发起GET异步请求         /// </summary>         /// <param name="url"></param>         /// <param name="headers"></param>         /// <param name="contentType"></param>         /// <returns></returns>         public static async Task<T> HttpGetAsync<T>(string url, string contentType = "application/json", Dictionary<string, string> headers = null)         {             var res = await HttpGetAsync(url, contentType, headers);             return res.ToEntity<T>();         }     }      public static class HttpExtension     {         /// <summary>         /// 发起GET同步请求         /// </summary>         /// <param name="client"></param>         /// <param name="url"></param>         /// <param name="headers"></param>         /// <param name="contentType"></param>         /// <returns></returns>         public static string HttpGet(this System.Net.Http.HttpClient client, string url, string contentType = "application/json",                                      Dictionary<string, string> headers = null)         {             if (contentType != null)                 client.DefaultRequestHeaders.Add("ContentType", contentType);             if (headers != null)             {                 foreach (var header in headers)                     client.DefaultRequestHeaders.Add(header.Key, header.Value);             }             HttpResponseMessage response = client.GetAsync(url).Result;             return response.Content.ReadAsStringAsync().Result;         }          /// <summary>         /// 发起GET异步请求         /// </summary>         /// <param name="client"></param>         /// <param name="url"></param>         /// <param name="headers"></param>         /// <param name="contentType"></param>         /// <returns></returns>         public static async Task<string> HttpGetAsync(this System.Net.Http.HttpClient client, string url, string contentType = "application/json", Dictionary<string, string> headers = null)         {             if (contentType != null)                 client.DefaultRequestHeaders.Add("ContentType", contentType);             if (headers != null)             {                 foreach (var header in headers)                     client.DefaultRequestHeaders.Add(header.Key, header.Value);             }             HttpResponseMessage response = await client.GetAsync(url);             return await response.Content.ReadAsStringAsync();         }          /// <summary>         /// 发起POST同步请求         /// </summary>         /// <param name="client"></param>         /// <param name="url"></param>         /// <param name="postData"></param>         /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>         /// <param name="timeOut"></param>         /// <param name="headers">填充消息头</param>         /// <returns></returns>         public static string HttpPost(this System.Net.Http.HttpClient client, string url, string postData = null,                                       string contentType = "application/json", int timeOut = 30, Dictionary<string, string> headers = null)         {             postData = postData ?? "";             if (headers != null)             {                 foreach (var header in headers)                     client.DefaultRequestHeaders.Add(header.Key, header.Value);             }             using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))             {                 if (contentType != null)                     httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);                  HttpResponseMessage response = client.PostAsync(url, httpContent).Result;                 return response.Content.ReadAsStringAsync().Result;             }         }          /// <summary>         /// 发起POST异步请求         /// </summary>         /// <param name="client"></param>         /// <param name="url"></param>         /// <param name="postData"></param>         /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>         /// <param name="headers">填充消息头</param>         /// <returns></returns>         public static async Task<string> HttpPostAsync(this System.Net.Http.HttpClient client, string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary<string, string> headers = null)         {             postData = postData ?? "";             client.Timeout = new TimeSpan(0, 0, timeOut);             if (headers != null)             {                 foreach (var header in headers)                     client.DefaultRequestHeaders.Add(header.Key, header.Value);             }             using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))             {                 if (contentType != null)                     httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);                  HttpResponseMessage response = await client.PostAsync(url, httpContent);                 return await response.Content.ReadAsStringAsync();             }         }          /// <summary>         /// 发起POST同步请求         /// </summary>         /// <param name="url"></param>         /// <param name="postData"></param>         /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>         /// <param name="headers">填充消息头</param>         /// <returns></returns>         public static T HttpPost<T>(this System.Net.Http.HttpClient client, string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary<string, string> headers = null)         {             return client.HttpPost(url, postData, contentType, timeOut, headers).ToEntity<T>();         }          /// <summary>         /// 发起POST异步请求         /// </summary>         /// <param name="url"></param>         /// <param name="postData"></param>         /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>         /// <param name="headers">填充消息头</param>         /// <returns></returns>         public static async Task<T> HttpPostAsync<T>(this System.Net.Http.HttpClient client, string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary<string, string> headers = null)         {             var res = await client.HttpPostAsync(url, postData, contentType, timeOut, headers);             return res.ToEntity<T>();         }          /// <summary>         /// 发起GET同步请求         /// </summary>         /// <param name="url"></param>         /// <param name="headers"></param>         /// <param name="contentType"></param>         /// <returns></returns>         public static T HttpGet<T>(this System.Net.Http.HttpClient client, string url, string contentType = "application/json", Dictionary<string, string> headers = null)         {             return client.HttpGet(url, contentType, headers).ToEntity<T>();         }          /// <summary>         /// 发起GET异步请求         /// </summary>         /// <param name="url"></param>         /// <param name="headers"></param>         /// <param name="contentType"></param>         /// <returns></returns>         public static async Task<T> HttpGetAsync<T>(this System.Net.Http.HttpClient client, string url, string contentType = "application/json", Dictionary<string, string> headers = null)         {             var res = await client.HttpGetAsync(url, contentType, headers);             return res.ToEntity<T>();         }     }      public static class JsonExtends     {         public static T ToEntity<T>(this string val)         {             return JsonConvert.DeserializeObject<T>(val);         }         public static string ToJson<T>(this T entity, Formatting formatting = Formatting.None)         {             return JsonConvert.SerializeObject(entity, formatting);         }     } } 

返回数据压缩

  • 在Program.cs中添加下面代码

默认压缩

builder.Services.AddResponseCompression(); app.UseResponseCompression(); 

Gzip压缩

builder.Services.AddResponseCompression(config => {     config.Providers.Add<GzipCompressionProvider>(); });  builder.Services.Configure<GzipCompressionProviderOptions>(config => {     config.Level = CompressionLevel.SmallestSize; }); 

缓存

接口缓存

  • 使用特性或者直接在返回头中添加Cache-Control效果一样

[HttpGet] [ResponseCache(Duration = 600)] public IActionResult GetString() => new JsonResult(new { result = "Success" });  [HttpGet] public IActionResult GetStringEx()  {     HttpContext.Response.Headers.Add("Cache-Control", "public,max-age=600");     return new JsonResult(new { result = "Success" }); } 

静态文件缓存

app.UseStaticFiles(new StaticFileOptions() {     OnPrepareResponse = Prepare =>     {         Prepare.Context.Response.Headers.Add(HeaderNames.CacheControl, "public,max-age=600");     } });