随笔分类 -  WebApi

摘要:1.自动重定向 HttpClientHandler clientHandler = new HttpClientHandler() { AllowAutoRedirect = true, MaxAutomaticRedirections = 2 }; 是否重定向,最大重定向次数 HttpClient 阅读全文
posted @ 2021-12-27 05:39 zq爱生活爱代码 阅读(57) 评论(0) 推荐(0)
摘要:using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; namespace W 阅读全文
posted @ 2021-12-27 05:30 zq爱生活爱代码 阅读(59) 评论(0) 推荐(0)
摘要:1.使用当前登录用户 HttpClientHandler hch = new HttpClientHandler(); hch.UseDefaultCredentials = true; 2.使用账户密码,模拟任何账户 HttpClientHandler handler = new HttpClie 阅读全文
posted @ 2021-12-27 02:08 zq爱生活爱代码 阅读(140) 评论(0) 推荐(0)
摘要:转载 https://www.cnblogs.com/lizhenhong/p/10002601.html 1、Web.config配置上system.web节点下加入以下配置 <system.web> <authentication mode="Forms"> <forms name=".wech 阅读全文
posted @ 2021-11-28 09:28 zq爱生活爱代码 阅读(237) 评论(0) 推荐(0)
摘要:开始Form认证,拒绝匿名登录 <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> <authentication mode="Forms"> <f 阅读全文
posted @ 2021-11-22 19:45 zq爱生活爱代码 阅读(60) 评论(0) 推荐(0)
摘要:OverrideActionFilters 当我们在Global设定过滤器时,有些function不希望被卡住,那么就可以在function上面设定OverrideActionFilters ,不执行 在这之前,我们要知道ScopeFilter的执行顺序 Global=》Controller=》Ac 阅读全文
posted @ 2021-11-17 17:20 zq爱生活爱代码 阅读(93) 评论(0) 推荐(0)
摘要:1.如果是action中发生异常,那么 会先先逆着顺序执行 actionfilter的OnActionExecuted事件,最后再逆着执行 ExceptionFilterAttribute 的 OnException事件 通过参数HttpActionExecutedContext.Exception 阅读全文
posted @ 2021-11-17 16:39 zq爱生活爱代码 阅读(174) 评论(0) 推荐(0)
摘要:如果在ActionFilterAttribute的OnActionExecuting事件中返回一个 HttpResponseMessage 作为 参数HttpActionContext 的Response属性 那么会直接返回,甚至不会执行action。比如 验证未通过,直接返回 Response 如 阅读全文
posted @ 2021-11-17 08:08 zq爱生活爱代码 阅读(258) 评论(0) 推荐(0)
摘要:// 同一个action上多个actionfilter 会按照 顺序调用 OnActionExecuting 事件, 注意 OnActionExecuting 发生在 Model参数绑定之后,可以在OnActionExecuting 里面验证 actionContext.ModelState.IsV 阅读全文
posted @ 2021-11-16 18:52 zq爱生活爱代码 阅读(536) 评论(0) 推荐(0)
摘要:AuthorizationFilterAttribute 判定逻辑 重写在 OnAuthorization 里面 获取 请求验证的头信息 using System; using System.Collections.Generic; using System.Linq; using System.N 阅读全文
posted @ 2021-11-16 12:56 zq爱生活爱代码 阅读(97) 评论(0) 推荐(0)
摘要:在WEBAPI中获取 权限实体的方法 //this.ActionContext.RequestContext.Principal // 引用 相同User //HttpContext.Current.User; 注意上面进行赋值的时候,Thread.CurrentPrincipal 也会一并修改 阅读全文
posted @ 2021-11-15 19:55 zq爱生活爱代码 阅读(210) 评论(0) 推荐(0)
摘要:如果验证失败,会返回 401 Unauthorized 并且通过 response.Headers.WwwAuthenticate 头信息 获取 AuthenticationHeaderValue ,从而获取 验证规则,以及服务器传的值 比如 在程序中 给头信息赋值 credential,clien 阅读全文
posted @ 2021-11-15 19:53 zq爱生活爱代码 阅读(111) 评论(0) 推荐(0)
摘要:转载 https://blog.csdn.net/Fang_Yanchang/article/details/109377636?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_title~default-0.es 阅读全文
posted @ 2021-11-09 06:50 zq爱生活爱代码 阅读(47) 评论(0) 推荐(0)
摘要:Accept:响应可接收的媒体类型,如"application/json"、"application/xml",或者自定义媒体类型,如"application/vnd.example+xml"。 当我们希望接收到的是JSON时 private void button2_Click(object se 阅读全文
posted @ 2021-11-09 06:38 zq爱生活爱代码 阅读(130) 评论(0) 推荐(0)
摘要:有时候在api执行完一个方法后,我们还得再跳到另外一个方法执行其他操作。 这种情况下,我们可以调用方法,也可以根据情况进行重定向作业 private void button2_Click(object sender, EventArgs e) { HttpWebRequest request=(Ht 阅读全文
posted @ 2021-11-09 06:15 zq爱生活爱代码 阅读(1421) 评论(0) 推荐(0)
摘要:事件作用在Model绑定之后,action执行之前。有时候我们要检查model绑定的内容,比如是否为空,长度,大小,格式等等。 最简单的属性必选判定,需要在Model上面设定 [Required] 也可以设定异常说明的内容 http://localhost:20138/api/Demo public 阅读全文
posted @ 2021-11-08 07:31 zq爱生活爱代码 阅读(316) 评论(0) 推荐(0)
摘要:绑定action内参数对象的方法有很多种,无论是针对简单对象,还是复杂对象。比如 typeConverter 有兴趣的,可以看看我其他文章 但是如果需要在绑定参数的过程中进行某些验证,或者绑定对象的数据并不单单来源于用户传过来的值,比如获取请求的所有Cookie对象等等 这种情况的话,比 typeC 阅读全文
posted @ 2021-11-05 15:51 zq爱生活爱代码 阅读(255) 评论(0) 推荐(0)
摘要:設定模型系結器 有幾種方式可以設定模型系結器。 首先,您可以將 [ModelBinder] 屬性加入至參數。 public HttpResponseMessage Get([ModelBinder(typeof(GeoPointModelBinder))] GeoPoint location) 您也 阅读全文
posted @ 2021-11-03 17:21 zq爱生活爱代码 阅读(55) 评论(0) 推荐(0)
摘要:需要引用using System.Net.Http; var cookies = actionContext.Request.Headers.GetCookies(); // API 推荐 HttpCookieCollection collection = HttpContext.Current.R 阅读全文
posted @ 2021-11-02 14:55 zq爱生活爱代码 阅读(230) 评论(0) 推荐(0)
摘要:传递实体对象,首先定义负责对象 public class Student { public string name { get; set; } public string sex { get; set; } public string age { get; set; } public Adress 阅读全文
posted @ 2021-11-01 17:23 zq爱生活爱代码 阅读(479) 评论(0) 推荐(0)