HttpRequestMessage扩展方法
public static class HttpRequestMessageExtensions
{
/// <summary>
/// Gets the <see cref="HttpConfiguration"/> for the given request.
/// </summary>
/// <param name="request">The HTTP request.</param>
/// <returns>The <see cref="HttpConfiguration"/>.</returns>
public static HttpConfiguration GetConfiguration(this HttpRequestMessage request)
{
if (request == null)
{
throw new ArgumentException("request");
} return request.GetProperty<HttpConfiguration>(HttpPropertyKeys.HttpConfigurationKey);
} /// <summary>
/// Gets the <see cref="System.Threading.SynchronizationContext"/> for the given request or null if not available.
/// </summary>
/// <param name="request">The HTTP request.</param>
/// <returns>The <see cref="System.Threading.SynchronizationContext"/> or null.</returns>
public static SynchronizationContext GetSynchronizationContext(this HttpRequestMessage request)
{
if (request == null)
{
throw new ArgumentException("request");
} return request.GetProperty<SynchronizationContext>(HttpPropertyKeys.SynchronizationContextKey);
} /// <summary>
/// Gets the <see cref="System.Web.Http.Routing.IHttpRouteData"/> for the given request or null if not available.
/// </summary>
/// <param name="request">The HTTP request.</param>
/// <returns>The <see cref="System.Web.Http.Routing.IHttpRouteData"/> or null.</returns>
public static IHttpRouteData GetRouteData(this HttpRequestMessage request)
{
if (request == null)
{
throw new ArgumentException("request");
} return request.GetProperty<IHttpRouteData>(HttpPropertyKeys.HttpRouteDataKey);
} public static T GetProperty<T>(this HttpRequestMessage request, string key)
{
T value = default(T);
object @object = null;
request.Properties.TryGetValue(key, out @object);
if (@object is T)
{
value = (T)@object;
}
else
{
throw new InvalidCastException(string.Format("无效类型转换:{0}", typeof(T).FullName));
}
return value;
} }
HttpRequestMessage扩展方法的更多相关文章
- WebAPi添加常用扩展方法及思维发散
前言 在WebAPi中我们通常需要得到请求信息中的查询字符串或者请求头中数据再或者是Cookie中的数据,如果需要大量获取,此时我们应该想到封装一个扩展类来添加扩展方法,从而实现简便快捷的获取. We ...
- .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法
.NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...
- .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类
.NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类 0x00 为什么要引入扩展方法 有的中间件功能比较简单,有的则比较复杂,并且依赖其它组件.除 ...
- 为IEnumerable<T>添加RemoveAll<IEnumerable<T>>扩展方法--高性能篇
最近写代码,遇到一个问题,微软基于List<T>自带的方法是public bool Remove(T item);,可是有时候我们可能会用到诸如RemoveAll<IEnumerab ...
- C#的扩展方法解析
在使用面向对象的语言进行项目开发的过程中,较多的会使用到“继承”的特性,但是并非所有的场景都适合使用“继承”特性,在设计模式的一些基本原则中也有较多的提到. 继承的有关特性的使用所带来的问题:对象的继 ...
- 扩展方法(C#)
扩展方法使你能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型.扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用. 下面的示例为String添加 ...
- 扩展方法解决LinqToSql Contains超过2100行报错问题
1.扩展方法 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sy ...
- C#扩展方法
扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法就相当于一个马甲,给一个现有类套上,就可以为这个类添加其他方法了. 马甲必须定义为stati ...
- 枚举扩展方法获取枚举Description
枚举扩展方法 /// <summary> /// 扩展方法,获得枚举的Description /// </summary> /// <param name="v ...
随机推荐
- [日常] NOI2019 退役记
这次要彻底退役了 开个坑先 Day -2 出发坐车去gz 好像和上次去雅礼的车是同一趟于是大家都以为和上次一样是 \(10:40\) 开车, 于是提前2h大概八点多就去坐公交了 到了之后取票, 发现票 ...
- python 学习常见问题笔记
1.for...if...构建List segs = [v for v in segs if not str(v).isdigit()]#去数字 https://www.cnblogs.com/eni ...
- 将Excel表格数据转换成Datatable
/// <summary> /// 将Excel表格数据转换成Datatable /// </summary> /// <param name="fileUrl ...
- Spring Cloud Ribbon客户端负载均衡(四)
序言 Ribbon 是一个客户端负载均衡器(Nginx 为服务端负载均衡),它赋予了应用一些支配 HTTP 与 TCP 行为的能力,可以得知,这里的客户端负载均衡也是进程内负载均衡的一种.它在 Spr ...
- Asp.Net后台弹出确认提示窗Confirm
前端js代码: function MyConfirm(message, guid) { if (confirm(message) == true) { document.getElementById( ...
- ASP.NET list<object> OBJECT.clean()会清空session['OBJECT']的值的问题
public partial class 测试 : System.Web.UI.Page { static List<Item> allAnswer= new List<Item&g ...
- elasticsearch7 配置篇
学习了这么多,终于开始搭建生产环境了,这一篇主要讲解配置项,以及支持中文分词的ik安装,集群的搭建. 配置项确实挺多的,但把几个常用配置熟悉就好,而且就像elasticsearch官方文档所说,不存在 ...
- android studio学习----gradle命令详解
首先来给大家介绍一种简便并且个人最喜欢的一种办法.很多时候我们在GitHub上看到一个不错的开源项目,一般有两种需求,阅读源码和查看运行效果,如果是单纯的查看源码我更喜欢用一些轻量级编辑器,如vim, ...
- wc项目(node.js实现)
一.github地址:https://github.com/Jasminejiamei/homework-wc 二.PSP PSP Personal Software Process Stages 预 ...
- Linux系统下安装jdk及环境配置(两种方法)
https://blog.csdn.net/qq_42815754/article/details/82968464 这里介绍两种linux环境下jdk的安装以及环境配置方法在windows系统安装j ...