webapi

netcore3.1配置webapi Controller打开报错

偶尔善良 提交于 2020-03-01 07:53:46
问题描述:刚配置好了swagger,沿用netcore2.2的配置继续在浏览器打开api控制器,结果报错了。。。 解决方法:从路由层面查找了半天原因,无果,最后把api页面中的跨域设置去掉后正常了,看来和跨越设置有关,继续查找,原来是注册时候顺序导致的问题 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseStaticFiles(); app.UseCookiePolicy(); app.UseRouting(); //允许跨域 位置和必须放对,否则加载出错 app.UseCors("any"); app.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute()); //启用中间件服务生成Swagger作为JSON终结点 app.UseSwagger(); //启用中间件服务对swagger-ui,指定Swagger JSON终结点 app.UseSwaggerUI(c => { c

WebAPi添加常用扩展方法及思维发散

て烟熏妆下的殇ゞ 提交于 2020-02-28 14:45:34
前言 在WebAPi中我们通常需要得到请求信息中的查询字符串或者请求头中数据再或者是Cookie中的数据,如果需要大量获取,此时我们应该想到封装一个扩展类来添加扩展方法,从而实现简便快捷的获取。 WebAPi常用扩展方法 (1)获取所有键值对 /// <summary> /// 获取所有键值 /// </summary> /// <param name="request"></param> /// <returns></returns> public static Dictionary<string, string> GetQueryStrings(this HttpRequestMessage request) { return request.GetQueryNameValuePairs().ToDictionary(k => k.Key, v => v.Value, StringComparer.OrdinalIgnoreCase); } (2)获取单个key对应value /// <summary> /// 获取单个键值 /// </summary> /// <param name="request"></param> /// <param name="key"></param> /// <returns></returns> public static string

第十一节:WebApi的版本管理的几种方式

这一生的挚爱 提交于 2020-02-28 13:53:12
一. 背景和方案 1. 多版本管理的概念   Android 、IOS等 App 存在着多版本客户端共存的问题:App 最新版已经升级到了5.0 了,但是有的用户手机上还运行着 4.8、3.9 甚至2.2 版本的 App,由于早期没有内置升级机制、用户不会升级、用户拒绝升级等原因,造成这些旧版本 App 也在运行。开发新版本 App 的时候,要给接口增加新的功能或者修改以前接口的规范,会造成旧版本App 无法使用,因此在一定情况下会“ 保留旧接口的运行、新功能用新接口 ”,这样就会存在多版本接口共存的问题。   通常的做法是:旧版接口做一个代码分支,除了进行 bug 修改外,旧版本接口不再做改动,新接口代码继续演化升级。在客户端请求的时候带着要请求的接口版本号,在服务器端选择合适的版本代码进行处理。 2. 解决方案 (1). 不同的版本使用不同的域名:v1.api.ypf.com、v2.api.ypf.com、v3…… (最佳方案) (2). 在Url,报文头等中带不同的版本信息,用Nginx等做反向代理服务,然后将 http://api.ypf.com/api/v1/User/1 和 http://api.ypf.com/api/v2/User/1 转到不同的服务器处理。 (3). 多个版本的 Controller共处在一个项目中,然 后使 用 [RoutePrefix]

第十二节:WebApi自动生成在线Api文档的两种方式

生来就可爱ヽ(ⅴ<●) 提交于 2020-02-28 13:51:20
一. WebApi自带生成api文档 1. 说明   通过观察,发现WebApi项目中Area文件夹下有一个HelpPage文件夹,如下图,该文件夹就是WebApi自带的生成Api的方式,如果该文件夹没了,可以通过Nuget安装:Microsoft.AspNet.WebApi.HelpPage ,你就会发现下图这一坨代码又回来了。    使用:http://localhost:2131/Help/Index , 即可访问生成的Api目录,如下图: 缺点:你会发现一个很坑爹的问题,方法名的注释和参数的注释均不显示,这对使用者而言,相当不放方便了。 2. 改进支持参数的注释 (1). 选中项目,右键属性,填写生成xml文件的路径,如下图: bin\api.xml (2). 找到 Areas/HelpPage/App_Start 目录下的HelpPageConfig.cs 文件,Register 方法,添加一行代码: config.SetDocumentationProvider(new XmlDocumentationProvider(AppDomain.CurrentDomain.BaseDirectory + "bin\\api.xml")); (3). 大功告成,再次访问 http://localhost:2131/Help/Index , 发现无论是方法名还是参数名,均有描述了

webapi使用JWT进行授权认证(一)

一曲冷凌霜 提交于 2020-02-28 00:59:08
1,在asp.net mvc 中常用的是 Cooke+Session 或者 form 认证(实际也是Cooke)的方式,当然都是mvc中在 webapi中也是可以使用的,那么上面的两种方式这里就不多写了,后面有时间写,今天最主要是 JWT的认证方式 2,简单说明一下,JWT是什么。JWT 全称 JSON Web Tokens ,是一种规范化的 token。是对 token 这一技术提出一套规范,其它细节,查资料就可。 3,使用,.NET里面可以使用JWT来生成Token以及解密Token。我们打开Nuget,搜索JWT 安装。 4,既然是根据同koken去获取权限,那么第一个就是去获取token private string secret = "zhonlong"; //这个密钥以一般用加密过的 public string Get() { IDateTimeProvider provider = new UtcDateTimeProvider(); var now = provider.GetNow(); var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); // or use JwtValidator.UnixEpoch var secondsSinceEpoch = Math.Round(

asp.net core3.0 webapi request.body and [frombody] conflict [closed]

杀马特。学长 韩版系。学妹 提交于 2020-02-25 13:22:06
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed last month . I need to get the body through request.body after frombody, but I haven't found a solution after testing for 2 days. I've added Request.EnableBuffering() . // PUT: api/Test/5 [HttpPut("{id}")] public async Task<string> PutAsync(int id, [FromBody]ProductInfo value) { var ccccc = ""; Request

asp.net core3.0 webapi request.body and [frombody] conflict [closed]

Deadly 提交于 2020-02-25 13:17:31
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed last month . I need to get the body through request.body after frombody, but I haven't found a solution after testing for 2 days. I've added Request.EnableBuffering() . // PUT: api/Test/5 [HttpPut("{id}")] public async Task<string> PutAsync(int id, [FromBody]ProductInfo value) { var ccccc = ""; Request

asp.net core3.0 webapi request.body and [frombody] conflict [closed]

岁酱吖の 提交于 2020-02-25 13:17:08
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed last month . I need to get the body through request.body after frombody, but I haven't found a solution after testing for 2 days. I've added Request.EnableBuffering() . // PUT: api/Test/5 [HttpPut("{id}")] public async Task<string> PutAsync(int id, [FromBody]ProductInfo value) { var ccccc = ""; Request

.net WebApi中使用swagger

梦想的初衷 提交于 2020-02-24 13:31:36
我在WebApi中使用swagger的时候发现会出现很多问题,搜索很多地方都没找到完全解决问题的方法,后面自己解决了,希望对于遇到同样问题朋友有帮助。我将先一步一步的演示项目中解决swagger遇到问题及解决方法。   首先我们新建一个api项目 图1 (默认生成项目) 图2(运行首页) 图3(默认Api列表) 图4(默认Get的Api) 以上图1-4都是默认情况下生成页面看起来不是那么好看,而且测试也不方便,下面将介绍怎么使用swagger。   使用nuget包获取Swashbule、swagger的包并安装。 图5(Swashbule - swagger for Api) 图6(swagger UI for net) 一般安装到就应该可以正常运行了。但是运行后我们发现抛出异常 图7(异常1) 打开解决方案属性-->生成,勾选XML文档文件,保存就ok。 图8 图9(异常2) 出现该异常是由于没有增加依赖项,大家可以自行查看自己的dll文件版本,做出修改,把下面的代码插入到web.config中。 1 2 3 4 5 6 7 8 9 10 11 12 <dependentAssembly> <assemblyIdentity name= "System.Net.Http.Formatting" publicKeyToken= "31bf3856ad364e35" culture

asp.net 引用webapi接收返回的Json类型数据

我是研究僧i 提交于 2020-02-23 10:51:50
在网上查到了一些例子加上自己的一些修改,改写了这个方法 private async Task < string > InvokeWebapi ( string strurl , string strapi , string strtype , string strJson ) { HttpClient client = new HttpClient ( ) ; //client.DefaultRequestHeaders.Add("",""); client . BaseAddress = new Uri ( strurl ) ; client . Timeout = TimeSpan . FromSeconds ( 510 ) ; string result = "" ; if ( strtype . ToLower ( ) == "post" ) { // var content = new FormUrlEncodedContent(dics); var content = new StringContent ( strJson ) ; content . Headers . ContentType = new MediaTypeHeaderValue ( "application/json" ) ; content . Headers . ContentLength = (