2 Star 3 Fork 2

zheng_qi/我的仓库

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
NotNull.cs 10.08 KB
一键复制 编辑 原始数据 按行查看 历史
zheng_qi 提交于 2021-11-08 03:21 . 取值错误处理
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Metadata;
using System.Web.Http.ModelBinding;
using System.Web.Http.Routing;
using System.Web.Http.Validation;
using System.Web.Http.ValueProviders;
using System.Web.Http.ValueProviders.Providers;
using Elmah;
namespace CatchGlobalException.Controllers
{
/// <summary>
///
/// </summary>
public class NotNull : ParameterBindingAttribute
{
public override HttpParameterBinding GetBinding(HttpParameterDescriptor parameter)
{
//parameter.Configuration.Formatters
//parameter.BindWithModelBinding();
//parameter.Configuration.act
//var aa = new DefaultActionValueBinder().GetBinding(parameter);
//System.Web.Http.ValueProviders.Providers.RouteDataValueProviderFactory
//return new MyFromBodyAttribute().GetBinding(parameter);
return TypeHelper.CanConvertFromString(parameter.ParameterType)
? parameter.BindWithAttribute((ParameterBindingAttribute) new MyFromUriAttribute())
: new MyFromBodyAttribute().GetBinding(parameter);
}
}
/// <summary>一个特性,该特性指定操作参数来自传入 <see cref="T:System.Net.Http.HttpRequestMessage" /> 的 URI。</summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]
public class MyFromUriAttribute : ModelBinderAttribute
{
/// <summary>获取模型联编程序的值提供程序工厂。</summary>
/// <returns>
/// <see cref="T:System.Web.Http.ValueProviders.ValueProviderFactory" /> 对象的集合。</returns>
/// <param name="configuration">配置。</param>
public override IEnumerable<ValueProviderFactory> GetValueProviderFactories(
HttpConfiguration configuration)
{
if (configuration == null)
throw new ArgumentNullException(nameof(configuration));
//foreach (ValueProviderFactory valueProviderFactory in base.GetValueProviderFactories(configuration))
//{
// if (valueProviderFactory is IUriValueProviderFactory)
// yield return valueProviderFactory;
//}
ValueProviderFactory[] valueProviders = {new RouteDataAndQueryStringProviderFactory()};
return valueProviders;
}
}
/// <summary> 一个特性,该特性指定操作参数仅来自传入 <see cref="T:System.Net.Http.HttpRequestMessage" /> 的实体正文。</summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]
public class MyFromBodyAttribute : ParameterBindingAttribute
{
/// <summary>获取参数绑定。</summary>
/// <returns>参数绑定。</returns>
/// <param name="parameter">参数说明。</param>
public override HttpParameterBinding GetBinding(HttpParameterDescriptor parameter)
{
if (parameter == null)
throw new ArgumentNullException(nameof(parameter));
IEnumerable<MediaTypeFormatter> formatters = (IEnumerable<MediaTypeFormatter>)parameter.Configuration.Formatters;
IBodyModelValidator bodyModelValidator = parameter.Configuration.Services.GetBodyModelValidator();
return (HttpParameterBinding) new MyFormatterParameterBinding(parameter, formatters, bodyModelValidator);
}
}
public class MyFormatterParameterBinding : FormatterParameterBinding
{
public MyFormatterParameterBinding(HttpParameterDescriptor descriptor, IEnumerable<MediaTypeFormatter> formatters, IBodyModelValidator bodyModelValidator)
: base(descriptor, formatters, bodyModelValidator)
{
}
public override async Task ExecuteBindingAsync(
ModelMetadataProvider metadataProvider,
HttpActionContext actionContext,
CancellationToken cancellationToken)
{
await base.ExecuteBindingAsync(metadataProvider, actionContext, cancellationToken);
var value = actionContext.ActionArguments[Descriptor.ParameterName];
// 判断参数是否为null
if (value == null)
{
throw new ArgumentNullException($"The parameter name is {Descriptor.ParameterName}.");
}
}
}
public class RouteDataAndQueryStringProviderFactory: ValueProviderFactory
{
//private const string RequestLocalStorageKey = "{C0E50671-A1D4-429E-93C9-2AA63779924F}";
//public override IValueProvider GetValueProvider(HttpActionContext actionContext)
//{
// IDictionary<string, object> properties = actionContext.Request.Properties;
// object dataValueProvider;
// if (!properties.TryGetValue("{C0E50671-A1D4-429E-93C9-2AA63779924F}", out dataValueProvider))
// {
// dataValueProvider = new RouteDataAndQueryStringValueProvider(actionContext, CultureInfo.InvariantCulture);
// properties["{C0E50671-A1D4-429E-93C9-2AA63779924F}"] = (object) dataValueProvider;
// }
// return (IValueProvider) dataValueProvider;
//}
//private const string RequestLocalStorageKey = "{8572540D-3BD9-46DA-B112-A1E6C9086003}";
private const string MyRequestLocalStorageKey = "{8572540D-3BD9-46DA-B112-A1E6C9fdsafsd}";
/// <summary>检索指定控制器上下文的值提供程序对象。</summary>
/// <returns>查询字符串值提供程序对象。</returns>
/// <param name="actionContext">一个对象,该对象封装有关当前 HTTP 请求的信息。</param>
//public override IValueProvider GetValueProvider(HttpActionContext actionContext)
//{
// IDictionary<string, object> properties = actionContext.Request.Properties;
// object stringValueProvider;
// if (!properties.TryGetValue("{8572540D-3BD9-46DA-B112-A1E6C9086003}", out stringValueProvider))
// {
// stringValueProvider = new RouteDataAndQueryStringValueProvider(actionContext, CultureInfo.InvariantCulture);
// properties["{8572540D-3BD9-46DA-B112-A1E6C9086003}"] = (object) stringValueProvider;
// }
// return (IValueProvider) stringValueProvider;
//}
/// <summary>检索指定控制器上下文的值提供程序对象。</summary>
/// <returns>查询字符串值提供程序对象。</returns>
/// <param name="actionContext">一个对象,该对象封装有关当前 HTTP 请求的信息。</param>
public override IValueProvider GetValueProvider(HttpActionContext actionContext)
{
IDictionary<string, object> properties = actionContext.Request.Properties;
object stringValueProvider;
if (!properties.TryGetValue(MyRequestLocalStorageKey, out stringValueProvider))
{
var routeValues =
RouteDataAndQueryStringValueProvider.GetRouteValues(actionContext.ControllerContext.RouteData);
var stringValues = actionContext.ControllerContext.Request.GetQueryNameValuePairs();
var values = routeValues.Concat(stringValues);
stringValueProvider = new RouteDataAndQueryStringValueProvider(values, CultureInfo.InvariantCulture);
properties[MyRequestLocalStorageKey] = stringValueProvider;
}
return (IValueProvider) stringValueProvider;
}
}
public class RouteDataAndQueryStringValueProvider : NameValuePairsValueProvider
{
public static IEnumerable<KeyValuePair<string, string>> GetRouteValues(
IHttpRouteData routeData)
{
foreach (KeyValuePair<string, object> keyValuePair in (IEnumerable<KeyValuePair<string, object>>) routeData.Values)
{
string str = keyValuePair.Value == null ? (string) null : keyValuePair.Value.ToString();
yield return new KeyValuePair<string, string>(keyValuePair.Key, str);
}
}
public RouteDataAndQueryStringValueProvider(IEnumerable<KeyValuePair<string, string>> values, CultureInfo culture)
: base(values, culture)
{
}
public override ValueProviderResult GetValue(string key)
{
var result = base.GetValue(key);
if (result == null)
{
throw new ArgumentNullException($"The parameterName is {key}.");
}
return result;
}
}
//public class MyRouteDataValueProviderFactory : RouteDataValueProviderFactory
//{
// public override IValueProvider GetValueProvider(HttpActionContext actionContext)
// {
// return base.GetValueProvider(actionContext);
// }
//}
//public class MyRouteDataValueProvider : RouteDataValueProvider
//{
// public MyRouteDataValueProvider(HttpActionContext actionContext, CultureInfo culture)
// : base(actionContext, culture)
// {
// }
// public override ValueProviderResult GetValue(string key)
// {
// var result= base.GetValue(key);
// return result;
// }
//}
//public class MyQueryStringValueProviderFactory : QueryStringValueProviderFactory
//{
// public override IValueProvider GetValueProvider(HttpActionContext actionContext)
// {
// return new MyQueryStringValueProvider(actionContext, null);
// }
//}
//public class MyQueryStringValueProvider : QueryStringValueProvider
//{
// public MyQueryStringValueProvider(HttpActionContext actionContext, CultureInfo culture)
// : base(actionContext, culture)
// {
// //NameValuePairsValueProvider
// //NameValuePairsValueProvider
// }
//}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/youlicc/Learn.git
git@gitee.com:youlicc/Learn.git
youlicc
Learn
我的仓库
master

搜索帮助